Skip to content

Commit 0817df1

Browse files
committed
Translation
Translation js/advanced-functions/bind/article
1 parent 44b8253 commit 0817df1

File tree

1 file changed

+48
-46
lines changed

1 file changed

+48
-46
lines changed

1-js/06-advanced-functions/10-bind/article.md

Lines changed: 48 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ libs:
55

66
# Function binding
77

8-
When passing object methods as callbacks, for instance to `setTimeout`, there's a known problem: "losing `this`".
8+
Ketika mengirimkan metode objek sebagai callback, seperti `setTimeout`, terdapat sebuah masalah: "kehilangan `this`".
99

10-
In this chapter we'll see the ways to fix it.
10+
Didalam chapter ini kita akan belajar cara memperbaikinya.
1111

12-
## Losing "this"
12+
## Kehilangan "this"
1313

14-
We've already seen examples of losing `this`. Once a method is passed somewhere separately from the object -- `this` is lost.
14+
Kita sudah melihat beberapa contoh saat kehilangan `this`. Sekalinya sebuah metode dikirim kebagian kode lain dengan terpisah dari objeknya -- `this` akan menghilang dari metodenya.
1515

16-
Here's how it may happen with `setTimeout`:
16+
Ini adalah bagaimana hal itu terjadi dengan `setTimeout`:
1717

1818
```js run
1919
let user = {
@@ -28,22 +28,22 @@ setTimeout(user.sayHi, 1000); // Hello, undefined!
2828
*/!*
2929
```
3030

31-
As we can see, the output shows not "John" as `this.firstName`, but `undefined`!
31+
Seperti yang bisa kita lihat, keluarannya tidak menampilkan "John" sebagai `this.firstName`, tapi menampilkan `undefined`!
3232

33-
That's because `setTimeout` got the function `user.sayHi`, separately from the object. The last line can be rewritten as:
33+
Itu karena `setTimeout` mendapatkan fungsi `user.sayHi`, terpisah dari objeknya. Baris terakhir bisa ditulis ulang sebagai:
3434

3535
```js
3636
let f = user.sayHi;
37-
setTimeout(f, 1000); // lost user context
37+
setTimeout(f, 1000); // kehilangan konteks dari user
3838
```
3939

40-
The method `setTimeout` in-browser is a little special: it sets `this=window` for the function call (for Node.js, `this` becomes the timer object, but doesn't really matter here). So for `this.firstName` it tries to get `window.firstName`, which does not exist. In other similar cases, usually `this` just becomes `undefined`.
40+
Metode `setTimeout` didalam peramban sedikit spesial: metode tersebut menyetel `this=window` untuk pemanggilan fungsi (untuk Node.js, `this` menjadi objek timer, tapi tidak terlalu penting disini). Jadi untuk `this.firstName` metodenya jadi mendapatkan `window.firstName`, yang mana tidak ada. Dalam kasus serupa lainnya `this` akan menjadi `undefined`.
4141

42-
The task is quite typical -- we want to pass an object method somewhere else (here -- to the scheduler) where it will be called. How to make sure that it will be called in the right context?
42+
Tugasnya cukup tipikal -- kita ingin mengirim metode objek ke bagian kode lainnya (disini -- kepada penjadwal/setTimeout) dimana metodenya akan dipanggil. Bagaimana cara untuk memeriksa konteksnya dipanggil dengan benar?
4343

44-
## Solution 1: a wrapper
44+
## Solusi 1: pembungkus
4545

46-
The simplest solution is to use a wrapping function:
46+
Solusi sederhananya adalah untuk menggunakan fungsi pembungkus:
4747

4848
```js run
4949
let user = {
@@ -60,17 +60,17 @@ setTimeout(function() {
6060
*/!*
6161
```
6262

63-
Now it works, because it receives `user` from the outer lexical environment, and then calls the method normally.
63+
Kode diatas bekerja, karena `user` didapatkan dari lingkungan leksikal terluar, dan lalu memanggil metodenya secara normal.
6464

65-
The same, but shorter:
65+
Solusi yang sama, tapi lebih pendek:
6666

6767
```js
6868
setTimeout(() => user.sayHi(), 1000); // Hello, John!
6969
```
7070

71-
Looks fine, but a slight vulnerability appears in our code structure.
71+
Terlihat bagus, tapi sedikit memiliki kerentanan yang akan muncul pada struktur kodenya.
7272

73-
What if before `setTimeout` triggers (there's one second delay!) `user` changes value? Then, suddenly, it will call the wrong object!
73+
Bagaimana jika sebelum `setTimeout` berjalan (terdapat penundaan selama satu detik!) nilai `user` untuk berubah? Maka, tiba-tiba,fungsinya akan memanggil objek yang salah.
7474

7575

7676
```js run
@@ -83,32 +83,32 @@ let user = {
8383

8484
setTimeout(() => user.sayHi(), 1000);
8585

86-
// ...the value of user changes within 1 second
86+
// ...nilai dari user berubah sebelum 1 detik!
8787
user = {
8888
sayHi() { alert("Another user in setTimeout!"); }
8989
};
9090

91-
// Another user in setTimeout!
91+
// setTimeout menggunakan user yang berbeda!
9292
```
9393

94-
The next solution guarantees that such thing won't happen.
94+
Solusi selanjutnya akan menjamin hal seperti diatas tidak akan terjadi.
9595

96-
## Solution 2: bind
96+
## Solusi 2: bind
9797

98-
Functions provide a built-in method [bind](mdn:js/Function/bind) that allows to fix `this`.
98+
Fungsi menyediakan sebuah metode bawaan [bind](mdn:js/Function/bind) yang mengijinkan untuk membernarkan `this`.
9999

100-
The basic syntax is:
100+
Sintaks dasarnya adalah:
101101

102102
```js
103-
// more complex syntax will come a little later
103+
// contoh sintaks yang lebih kompleks akan kita segera lihat
104104
let boundFunc = func.bind(context);
105105
```
106106

107-
The result of `func.bind(context)` is a special function-like "exotic object", that is callable as function and transparently passes the call to `func` setting `this=context`.
107+
hasil dari `func.bind(contenxt)` adalah sesuatu yang terlihat seperti fungsi spesial atau bisa disebut dengan "objek eksotik", yang dapat dipanggil sebagai fungsi dan dapat melanjutkan pemanggilan kepada `func` sambil menyetel `this=context`.
108108

109-
In other words, calling `boundFunc` is like `func` with fixed `this`.
109+
Dengan kata lain, memanggil `boundFunc` sama seperti `func` dengan nilai `this` yang tetap.
110110

111-
For instance, here `funcUser` passes a call to `func` with `this=user`:
111+
Contoh, disini `funcUser` mengirimkan sebuah panggilan kepada `func` dengan `this=user`:
112112

113113
```js run
114114
let user = {
@@ -125,9 +125,9 @@ funcUser(); // John
125125
*/!*
126126
```
127127

128-
Here `func.bind(user)` as a "bound variant" of `func`, with fixed `this=user`.
128+
Disini `func.bin(user)` sebagai sebuah varian dari `func`, dengan nilai tetap `this=user`.
129129

130-
All arguments are passed to the original `func` "as is", for instance:
130+
Seluruh argumen dikirim kepada `func` asli "sebagaimana adanya", contoh:
131131

132132
```js run
133133
let user = {
@@ -142,11 +142,11 @@ function func(phrase) {
142142
let funcUser = func.bind(user);
143143

144144
*!*
145-
funcUser("Hello"); // Hello, John (argument "Hello" is passed, and this=user)
145+
funcUser("Hello"); // Hello, John (argumen "Hello" dikirim, dan this=user)
146146
*/!*
147147
```
148148

149-
Now let's try with an object method:
149+
Sekarang kita coba dengan menggunakan metode objek:
150150

151151

152152
```js run
@@ -161,21 +161,21 @@ let user = {
161161
let sayHi = user.sayHi.bind(user); // (*)
162162
*/!*
163163

164-
// can run it without an object
164+
// bisa dijalankan tanpa objek
165165
sayHi(); // Hello, John!
166166

167167
setTimeout(sayHi, 1000); // Hello, John!
168168

169-
// even if the value of user changes within 1 second
170-
// sayHi uses the pre-bound value which is reference to the old user object
169+
// bahkan jika nilai dari user berubah sebelum 1 detik
170+
// sayHi menggunakan nilai yang telah diikat, yang mana telah mereferensi kepada objek yang lama
171171
user = {
172172
sayHi() { alert("Another user in setTimeout!"); }
173173
};
174174
```
175175

176-
In the line `(*)` we take the method `user.sayHi` and bind it to `user`. The `sayHi` is a "bound" function, that can be called alone or passed to `setTimeout` -- doesn't matter, the context will be right.
176+
Didalam baris `(*)` kita menggunakan metode `user.sayHi` dan mengikatkannta kepada `user`. `sayHi` adalah sebuah fungsi "terikat", yang bisa dipanggil sendiri atau dikirimkan kepada `setTimeout` -- itu tidaklah penting, yang penting adalah konteksnya tepat.
177177

178-
Here we can see that arguments are passed "as is", only `this` is fixed by `bind`:
178+
Disini kita bisa melihat argumen yang dikirimkan "seperti adanya", hanya saja `this` nilainya menjadi tetap oleh `bind`:
179179

180180
```js run
181181
let user = {
@@ -187,12 +187,12 @@ let user = {
187187

188188
let say = user.say.bind(user);
189189

190-
say("Hello"); // Hello, John ("Hello" argument is passed to say)
191-
say("Bye"); // Bye, John ("Bye" is passed to say)
190+
say("Hello"); // Hello, John (argumen "Hello" dikirim untuk digunakan)
191+
say("Bye"); // Bye, John ("Bye" dikirim untuk digunakan)
192192
```
193193

194-
````smart header="Convenience method: `bindAll`"
195-
If an object has many methods and we plan to actively pass it around, then we could bind them all in a loop:
194+
````smart header="Metode yang bermanfaat: `bindAll`"
195+
Jika sebuah objek mempunyai beberapa metode dan kita berencana untuk mengirimkannya kebagian kode lain secara terus-menerus, kita bisa mengikatkannya didalam sebuah perulangan:
196196

197197
```js
198198
for (let key in user) {
@@ -202,32 +202,33 @@ for (let key in user) {
202202
}
203203
```
204204

205-
JavaScript libraries also provide functions for convenient mass binding , e.g. [_.bindAll(object, methodNames)](http://lodash.com/docs#bindAll) in lodash.
205+
Librari Javascript juga menyediakan fungsi untuk memudahkan pengikatan/binding masal, contoh [_.bindAll(object, methodNames)](http://lodash.com/docs#bindAll) didalam lodash.
206206
````
207207
208-
## Partial functions
208+
## Partial functions/Fungsi sebagian
209209
210-
Until now we have only been talking about binding `this`. Let's take it a step further.
210+
Sampai sekarang kita hanya berbicara tentang binding/pengikatan `this`. Ayo kita lihat lebih dalam.
211211
212-
We can bind not only `this`, but also arguments. That's rarely done, but sometimes can be handy.
212+
Kita bisa mengikat bukan hanya `this`, tapi juga argumen. Yang mana sangat jarang digunakan, tapi terkadang cukup mudah digunakan.
213213
214-
The full syntax of `bind`:
214+
Sintaks penuh dari `bind`:
215215
216216
```js
217217
let bound = func.bind(context, [arg1], [arg2], ...);
218218
```
219219
220-
It allows to bind context as `this` and starting arguments of the function.
220+
Yang mana mengijinkan kita untuk mengikat konteks sebagai `this` dan memulai argumen dari sebuah fungsi.
221221
222222
For instance, we have a multiplication function `mul(a, b)`:
223+
Contoh, kita mempunyai sebuah fungsi perkalian `mul(a, b)`:
223224
224225
```js
225226
function mul(a, b) {
226227
return a * b;
227228
}
228229
```
229230
230-
Let's use `bind` to create a function `double` on its base:
231+
Kita gunakan `bind` untuk membuat sebuah fungsi `double` didalamnya:
231232
232233
```js run
233234
function mul(a, b) {
@@ -244,6 +245,7 @@ alert( double(5) ); // = mul(2, 5) = 10
244245
```
245246
246247
The call to `mul.bind(null, 2)` creates a new function `double` that passes calls to `mul`, fixing `null` as the context and `2` as the first argument. Further arguments are passed "as is".
248+
Pemanggilan terhadap `mul.bind(null, 2)` membuat sebuah fungsi baru `double` yang mengirimkan pemanggilan terhadap `mul, memperbaiki `null` sebagai konteks dan `2` sebagai argumen pertamanya. Ar
247249
248250
That's called [partial function application](https://en.wikipedia.org/wiki/Partial_application) -- we create a new function by fixing some parameters of the existing one.
249251

0 commit comments

Comments
 (0)