You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
4
4
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.
6
6
7
7
## Object.prototype
8
8
9
-
Let's say we output an empty object:
9
+
katakan kita mengeluarkan sebuah objek kosong:
10
10
11
11
```js run
12
12
let obj = {};
13
13
alert( obj ); // "[object Object]" ?
14
14
```
15
15
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!
17
17
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.
19
19
20
-
Here's what's going on:
20
+
Inilah yang terjadi:
21
21
22
22

23
23
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:
25
25
26
26

27
27
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`.
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`:
42
42
43
43
```js run
44
44
alert(Object.prototype.__proto__); // null
45
45
```
46
46
47
-
## Other built-in prototypes
47
+
## *prototype* bawaan lainnya
48
48
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*.
50
50
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.
52
52
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".
54
54
55
-
Here's the overall picture (for 3 built-ins to fit):
55
+
Ini adalah gambar keseluruhan (memasangkan 3 fungsi):
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:
75
75
76
76
```js run
77
77
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
79
79
```
80
80
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.
82
82
83
83
84
84

85
85
86
86
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):
88
89
89
90

90
91
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.
92
93
93
94
```js run
94
95
functionf() {}
95
96
96
97
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
98
99
```
99
100
100
-
## Primitives
101
+
## Primitif-primitif
101
102
102
-
The most intricate thing happens with strings, numbers and booleans.
103
+
Hal yang paling rumit terjadi dengan *string*, *number* dan *boolean*.
103
104
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.
105
106
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`.
107
108
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.
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.
125
126
126
127
```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.
128
129
129
-
So, generally, modifying a native prototype is considered a bad idea.
130
+
Jadi, umumnya, memodifikasi prototype asli bisa dikatakan bukan ide bagus.
130
131
```
131
132
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*.**
133
134
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.
135
136
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.
137
138
138
-
For instance:
139
+
Contoh:
139
140
140
141
```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
143
144
144
145
String.prototype.repeat=function(n) {
145
-
//repeat the string n times
146
+
//ulangi stringnya n kali
146
147
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
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.
185
186
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`.
187
188
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.
189
190
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.
191
192
192
-
## Summary
193
+
## Ringkasan
193
194
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
-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