Skip to content

Commit 20e6d80

Browse files
authored
Merge pull request #192 from yoga1234/master
Translate
2 parents d87ca7e + cb01c77 commit 20e6d80

File tree

1 file changed

+62
-61
lines changed

1 file changed

+62
-61
lines changed
Lines changed: 62 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
1-
# Native prototypes
1+
# *Prototype* asli
22

3-
The `"prototype"` property is widely used by the core of JavaScript itself. All built-in constructor functions use it.
3+
Properti `"prototype"` adalah properti yang banyak digunakan oleh Javascript itu sendiri. Semua konstruktor fungsi menggunakannya.
44

5-
First we'll see at the details, and then how to use it for adding new capabilities to built-in objects.
5+
Pertama kita akan melihat lebih lengkapnya dan bagaimana cara menggunakannya untuk menambah kemampuan dari objek-objek bawaan.
66

77
## Object.prototype
88

9-
Let's say we output an empty object:
9+
katakan kita mengeluarkan sebuah objek kosong:
1010

1111
```js run
1212
let obj = {};
1313
alert( obj ); // "[object Object]" ?
1414
```
1515

16-
Where's the code that generates the string `"[object Object]"`? That's a built-in `toString` method, but where is it? The `obj` is empty!
16+
Dimanakah kode yang menghasilkan *string* `"[object Object]"`? Itu adalah metode bawaan `toString`, tapi diamanakah itu berara? `obj` tidak berisi apapun!
1717

18-
...But the short notation `obj = {}` is the same as `obj = new Object()`, where `Object` is a built-in object constructor function, with its own `prototype` referencing a huge object with `toString` and other methods.
18+
...Tapi notasi pendek dari `obj = {}` sama seperti `obj = new Object()`, dimana `Object` adalah fungsi konstruktor objek bawaan, dengan properti `prototype`nya sendiri yang mereferensi sebuah objek besar `toString` dan metode lainnya.
1919

20-
Here's what's going on:
20+
Inilah yang terjadi:
2121

2222
![](object-prototype.svg)
2323

24-
When `new Object()` is called (or a literal object `{...}` is created), the `[[Prototype]]` of it is set to `Object.prototype` according to the rule that we discussed in the previous chapter:
24+
Ketika `new Object()` dipanggil (atau sebuah objek literal `{...}` dibuat), `[[Prototype]]`nya disetel ke `Object.prototype` mengikuti aturan yang telah kita bahas di bab sebelumnya:
2525

2626
![](object-prototype-1.svg)
2727

28-
So then when `obj.toString()` is called the method is taken from `Object.prototype`.
28+
Jadi ketika `obj.toString()` dipanggil metodenya dibawa dari `Object.prototype`.
2929

30-
We can check it like this:
30+
Kita bisa periksa seperti ini:
3131

3232
```js run
3333
let obj = {};
@@ -38,80 +38,81 @@ alert(obj.toString === obj.__proto__.toString); //true
3838
alert(obj.toString === Object.prototype.toString); //true
3939
```
4040

41-
Please note that there is no more `[[Prototype]]` in the chain above `Object.prototype`:
41+
Ingat bahwa disana sudah tidak ada lagi `[[Prototype]]` didalam rantai diatas `Object.prototype`:
4242

4343
```js run
4444
alert(Object.prototype.__proto__); // null
4545
```
4646

47-
## Other built-in prototypes
47+
## *prototype* bawaan lainnya
4848

49-
Other built-in objects such as `Array`, `Date`, `Function` and others also keep methods in prototypes.
49+
Objek bawaan lainnya seperti `Array`, `Date`, `Function` dan lainnya juga tetap menyimpan metode didalam *prototype*.
5050

51-
For instance, when we create an array `[1, 2, 3]`, the default `new Array()` constructor is used internally. So `Array.prototype` becomes its prototype and provides methods. That's very memory-efficient.
51+
Contoh, ketika kita membuat sebuah *array* `[1, 2, 3]`, konstruktor bawaan `new Array()` digunakan secara internal. Jadi `Array.prototype` menjadi *prototype* miliknya dan menyediakan metode-metode. Itu akan membuatnya menjadi efisien dalam penggunaan memori.
5252

53-
By specification, all of the built-in prototypes have `Object.prototype` on the top. That's why some people say that "everything inherits from objects".
53+
Sebagaimana spesifikasinya, semua *prototype* bawaan memiliki `Object.prototype` diatasnya. Itulah kenapa beberapa orang berkata bahwa "semuanya diwarisi dari objek".
5454

55-
Here's the overall picture (for 3 built-ins to fit):
55+
Ini adalah gambar keseluruhan (memasangkan 3 fungsi):
5656

5757
![](native-prototypes-classes.svg)
5858

59-
Let's check the prototypes manually:
59+
Sekarang kita cek *prototype*nya secara manual:
6060

6161
```js run
6262
let arr = [1, 2, 3];
6363

64-
// it inherits from Array.prototype?
64+
// apakah diwarisi dari Array.prototype?
6565
alert( arr.__proto__ === Array.prototype ); // true
6666

67-
// then from Object.prototype?
67+
// maka dari Object.prototype?
6868
alert( arr.__proto__.__proto__ === Object.prototype ); // true
6969

70-
// and null on the top.
70+
// dan null diatasnya.
7171
alert( arr.__proto__.__proto__.__proto__ ); // null
7272
```
7373

74-
Some methods in prototypes may overlap, for instance, `Array.prototype` has its own `toString` that lists comma-delimited elements:
74+
Beberapa metode didalam *prototype* mungkin tumpang tindih, contoh, `Array.prototype` memiliki `toString`nya sendiri yang menyusun elemen yang dipisahkan dengan koma:
7575

7676
```js run
7777
let arr = [1, 2, 3]
78-
alert(arr); // 1,2,3 <-- the result of Array.prototype.toString
78+
alert(arr); // 1,2,3 <-- hasil dari Array.prototype.toString
7979
```
8080

81-
As we've seen before, `Object.prototype` has `toString` as well, but `Array.prototype` is closer in the chain, so the array variant is used.
81+
Seperti yang telah kita lihat, `Object.prototype` memiliki `toString` juga, tapi `Array.prototype` lebih dekat dengan rantainya, jadi varian dari *array* mungkin akan digunakan.
8282

8383

8484
![](native-prototypes-array-tostring.svg)
8585

8686

87-
In-browser tools like Chrome developer console also show inheritance (`console.dir` may need to be used for built-in objects):
87+
88+
Dialam alat *browser* seperti *Chrome Developer Conolse* juga menunjukan pewarisannya (`console.dir` mungkin butuh untuk digunakan seperti objek bawaan):
8889

8990
![](console_dir_array.png)
9091

91-
Other built-in objects also work the same way. Even functions -- they are objects of a built-in `Function` constructor, and their methods (`call`/`apply` and others) are taken from `Function.prototype`. Functions have their own `toString` too.
92+
Objek bawaan lainnya juga mungkin bekerja mirip seperti itu. Bahkan fungsi -- mereka adalah objek dari konstruktor bawaan `Function`, dan metode mereka (`call`/`apply` dan lainnya) juga diambil dari `Function.prototype`. Fungsi juga memiliki `toString` mereka masing-masing.
9293

9394
```js run
9495
function f() {}
9596

9697
alert(f.__proto__ == Function.prototype); // true
97-
alert(f.__proto__.__proto__ == Object.prototype); // true, inherit from objects
98+
alert(f.__proto__.__proto__ == Object.prototype); // true, warisan dari objek
9899
```
99100

100-
## Primitives
101+
## Primitif-primitif
101102

102-
The most intricate thing happens with strings, numbers and booleans.
103+
Hal yang paling rumit terjadi dengan *string*, *number* dan *boolean*.
103104

104-
As we remember, they are not objects. But if we try to access their properties, temporary wrapper objects are created using built-in constructors `String`, `Number` and `Boolean`. They provide the methods and disappear.
105+
Seperti yang kita ingat, mereka bukanlah objek. Tapi jika kita mencoba untuk mengakses propertinya, objek pembungkus sementara menggunakan konstruktor bawaan `String`, `Number` dan `Boolean`. Mereka menyediakan metodenya dan menghilang.
105106

106-
These objects are created invisibly to us and most engines optimize them out, but the specification describes it exactly this way. Methods of these objects also reside in prototypes, available as `String.prototype`, `Number.prototype` and `Boolean.prototype`.
107+
Objek-objek ini dibuat tak terlihat untuk kita dan kebanyakan mesin mengoptimalkan mereka, tapi spesifikasinya menjelaskannya juga seperti itu. Metode dari objek ini juga tinggal didalam *prototype*, tersedia sebagai `String.prototype`, `Number.prototype` dan `Boolean.prototype`.
107108

108-
```warn header="Values `null` and `undefined` have no object wrappers"
109-
Special values `null` and `undefined` stand apart. They have no object wrappers, so methods and properties are not available for them. And there are no corresponding prototypes either.
109+
```warn header="Nilai `null` dan `undefined` tidak memiliki objek pembungkus"
110+
Nilai spesial `null` dan `undefined` memiliki pendiriannya sendiri. Mereka tidak memiliki objek pembungkus, jadi metode dan properti tidak tersedia untuk mereka. Dan juga tidak terdapat prototypenya.
110111
```
111112
112113
## Changing native prototypes [#native-prototype-change]
113114
114-
Native prototypes can be modified. For instance, if we add a method to `String.prototype`, it becomes available to all strings:
115+
Prototipe asli bisa dimodifikasi. Contoh, jika kita menambahkan metode kepada `String.prototype`, itu akan menjadi tersedia untuk selurung *string*.
115116
116117
```js run
117118
String.prototype.show = function() {
@@ -121,32 +122,32 @@ String.prototype.show = function() {
121122
"BOOM!".show(); // BOOM!
122123
```
123124

124-
During the process of development, we may have ideas for new built-in methods we'd like to have, and we may be tempted to add them to native prototypes. But that is generally a bad idea.
125+
Selama proses pembangunan, kita mungkin memiliki ide untuk metode bawaan baru yang kita ingin punya, dan kita mungkin tergoda untuk menambahkannya sebagai *prototype* asli. Tapi itu sebenarnya bukan ide yang bagus.
125126

126127
```warn
127-
Prototypes are global, so it's easy to get a conflict. If two libraries add a method `String.prototype.show`, then one of them will be overwriting the method of the other.
128+
Prototype terlihat di global, jadi akan mudah membuat konflik. Jika dua library menambahkan sebuah metode `String`prototype.show`, maka salah satu dari mereka akan menimpah yang lainnya.
128129
129-
So, generally, modifying a native prototype is considered a bad idea.
130+
Jadi, umumnya, memodifikasi prototype asli bisa dikatakan bukan ide bagus.
130131
```
131132

132-
**In modern programming, there is only one case where modifying native prototypes is approved. That's polyfilling.**
133+
**Didalam *programming* modern, terdapat satu kasus dimana memodifikasi *prototype* asli dapat diterima. Disebut dengan *polyfilling*.**
133134

134-
Polyfilling is a term for making a substitute for a method that exists in the JavaScript specification, but is not yet supported by a particular JavaScript engine.
135+
*Polyfilling* adalah sebuah istilah untuk membuat sebuah metode pengganti yang ada didalam spesifikasi Javascript, tapi itu tidak didukung oleh mesin Javascript tertentu.
135136

136-
We may then implement it manually and populate the built-in prototype with it.
137+
Kita mungkin mengimplementasi manual dan mengisi prototype bawaan dengan itu.
137138

138-
For instance:
139+
Contoh:
139140

140141
```js run
141-
if (!String.prototype.repeat) { // if there's no such method
142-
// add it to the prototype
142+
if (!String.prototype.repeat) { // Jika tidak terdapat metode
143+
// tambahkan kedalam prototype
143144

144145
String.prototype.repeat = function(n) {
145-
// repeat the string n times
146+
// ulangi stringnya n kali
146147

147-
// actually, the code should be a little bit more complex than that
148-
// (the full algorithm is in the specification)
149-
// but even an imperfect polyfill is often considered good enough
148+
// sebenarnya, kodenya haruslah lebih rumit dari itu
149+
// (algoritma lengkapnya ada didalam spesifikasinya)
150+
// bahkan sebuah polyfill tidak sempurna kadang bisa dikatakan cukup bagus
150151
return new Array(n + 1).join(this);
151152
};
152153
}
@@ -155,15 +156,15 @@ alert( "La".repeat(3) ); // LaLaLa
155156
```
156157

157158

158-
## Borrowing from prototypes
159+
## meminjam dari *prototype*
159160

160-
In the chapter <info:call-apply-decorators#method-borrowing> we talked about method borrowing.
161+
Didalam bab <info:call-apply-decorators#method-borrowing> kita berbicara tentang peminjaman metode.
161162

162-
That's when we take a method from one object and copy it into another.
163+
Itulah ketika kita mengambil metode dari satu objek dan menyalinnya ke objek lain.
163164

164-
Some methods of native prototypes are often borrowed.
165+
Beberapa metode dari *prototype* asli sering dipinjam.
165166

166-
For instance, if we're making an array-like object, we may want to copy some `Array` methods to it.
167+
Contoh, jika kita membuat objek yang mirip array, kita mungkin ingin menyalin beberapa metode `Array` darinya.
167168

168169
E.g.
169170

@@ -181,18 +182,18 @@ obj.join = Array.prototype.join;
181182
alert( obj.join(',') ); // Hello,world!
182183
```
183184

184-
It works because the internal algorithm of the built-in `join` method only cares about the correct indexes and the `length` property. It doesn't check if the object is indeed an array. Many built-in methods are like that.
185+
Contoh diatas bekerja karena algoritma internal bawaan `join` yang memperhatikan tentang indeks yang benar dan `length` dari properti. Itu tidak akan memeriksa apakah objeknya adalah array. Beberapa metode bawaan memang seperti itu.
185186

186-
Another possibility is to inherit by setting `obj.__proto__` to `Array.prototype`, so all `Array` methods are automatically available in `obj`.
187+
Kemungkinan lainnya adalah pewarisan dari `obj.__proto__` ke `Array.prototype`, jadi seluruh metode `Array` secara otomatis tersedia didalam `obj`.
187188

188-
But that's impossible if `obj` already inherits from another object. Remember, we only can inherit from one object at a time.
189+
Tapi itu menjadi tidak mungkin jika `obj` sudah mewarisi dari objek lainnya. Ingat, kita hanya bisa mewarisi dari satu objek pada satu waktu.
189190

190-
Borrowing methods is flexible, it allows to mix functionalities from different objects if needed.
191+
Meminjam metode sebenarnya cukup fleksibel, hal itu memperbolehkan kita untuk mencampur fungsionalitas dari objek yang berbeda-beda jika dibutuhkan.
191192

192-
## Summary
193+
## Ringkasan
193194

194-
- All built-in objects follow the same pattern:
195-
- The methods are stored in the prototype (`Array.prototype`, `Object.prototype`, `Date.prototype`, etc.)
196-
- The object itself stores only the data (array items, object properties, the date)
197-
- Primitives also store methods in prototypes of wrapper objects: `Number.prototype`, `String.prototype` and `Boolean.prototype`. Only `undefined` and `null` do not have wrapper objects
198-
- Built-in prototypes can be modified or populated with new methods. But it's not recommended to change them. The only allowable case is probably when we add-in a new standard, but it's not yet supported by the JavaScript engine
195+
- Seluruh objek bawaan mengikuti alur yang sama:
196+
- Metode disimpan didalam prototype (`Array.prototype`, `Object.prototype`, `Date.prototype`, etc.)
197+
- Objeknya sendiri hanya menyimpan data (item array, properti objek, tanggal)
198+
- Prototype Asli menyimpan metode didalam prototype dari objek pembungkus: `Number.prototype`, `String.prototype` dan `Boolean.prototype`. Only `undefined` dan `null` tidak memiliki objek pembungkus.
199+
- *Prototype* bawaan bisa dimodifikasi atau diisi ulang dengan metode baru. Tapi tidak direkomendasikan untuk mengubahnya. Hal yang diperbolehkan dalam beberapa kasus mungkun ketika kita menambahkan peraturan baru, tapi itu belum sepenuhnya didukung oleh mesin Javascript.

0 commit comments

Comments
 (0)