Skip to content

Commit d9a75a0

Browse files
committed
Translation
Tanslation js/advanced-functions/call-apply-decorators/...
1 parent 7899679 commit d9a75a0

File tree

13 files changed

+92
-91
lines changed

13 files changed

+92
-91
lines changed

1-js/06-advanced-functions/09-call-apply-decorators/01-spy-decorator/_js.view/solution.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
function spy(func) {
22

33
function wrapper(...args) {
4-
// using ...args instead of arguments to store "real" array in wrapper.calls
4+
// gunakan ...args daripada argumen untuk menyimpan "array asli" didalam wrapper.calls
55
wrapper.calls.push(args);
66
return func.apply(this, args);
77
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
function spy(func) {
2-
// your code
2+
// Tulis kodemu disini
33
}
44

55

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
The wrapper returned by `spy(f)` should store all arguments and then use `f.apply` to forward the call.
1+
Pembungkus yang dikembalikan oleh `spy(f)` harus menyimpan semua argumen dan lalu menggunakan `f.apply` untuk melanjutkan pemanggilannya.

1-js/06-advanced-functions/09-call-apply-decorators/01-spy-decorator/task.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
importance: 5
1+
nilai penting: 5
22

33
---
44

55
# Spy decorator
66

7-
Create a decorator `spy(func)` that should return a wrapper that saves all calls to function in its `calls` property.
7+
Buatlah sebuah dekorator `spy(func)` yang harus mengembalikan pembungkus yang menyimpan semua pemanggilan kepada fungsinya didalam propertinya sendiri bernama `calls`.
88

9-
Every call is saved as an array of arguments.
9+
Setiap pemanggilan disimpan sebagai sebuah array dari argumen.
1010

11-
For instance:
11+
Contoh:
1212

1313
```js
1414
function work(a, b) {
15-
alert( a + b ); // work is an arbitrary function or method
15+
alert( a + b ); // bayangkan work adalah sebuah fungsi yang panjang
1616
}
1717

1818
*!*
@@ -27,4 +27,4 @@ for (let args of work.calls) {
2727
}
2828
```
2929

30-
P.S. That decorator is sometimes useful for unit-testing. Its advanced form is `sinon.spy` in [Sinon.JS](http://sinonjs.org/) library.
30+
Catatan. Dekoratornya harus berguna untuk unit-testing. Bentuk lanjutannya adalah `sinon.spy` didalam librari [Sinon.JS](http://sinonjs.org/).

1-js/06-advanced-functions/09-call-apply-decorators/02-delay/solution.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
The solution:
1+
Solusi:
22

33
```js run demo
44
function delay(f, ms) {
@@ -11,22 +11,22 @@ function delay(f, ms) {
1111

1212
let f1000 = delay(alert, 1000);
1313

14-
f1000("test"); // shows "test" after 1000ms
14+
f1000("test"); // tampilkan "test" setelah 1000ms
1515
```
1616

17-
Please note how an arrow function is used here. As we know, arrow functions do not have own `this` and `arguments`, so `f.apply(this, arguments)` takes `this` and `arguments` from the wrapper.
17+
Perhatikan bagaimana fungsi arrow digunakan disini. Seperti yang kita tahu, fungsi panah tidak memiliki `this` dan `argumen`nya sendiri, jadi `f.apply(this, arguments)` akan mengambil `this` dan `arguments` dari pembungkusnya.
1818

19-
If we pass a regular function, `setTimeout` would call it without arguments and `this=window` (assuming we're in the browser).
19+
Jika kita memasukan fungsi yang biasa, `setTimeout` akan memanggil fungsinya tanpa argumen dan `this=window` (asumsikan kita berada didalam peramban).
2020

21-
We still can pass the right `this` by using an intermediate variable, but that's a little bit more cumbersome:
21+
Kita masih bisa memberikan `this` yang benar dengan menggunakan variabel tambahan, tapi kodenya akan sedikit menjadi lebih rumit:
2222

2323
```js
2424
function delay(f, ms) {
2525

2626
return function(...args) {
27-
let savedThis = this; // store this into an intermediate variable
27+
let savedThis = this; // simpan this kedalam variabel tambahan
2828
setTimeout(function() {
29-
f.apply(savedThis, args); // use it here
29+
f.apply(savedThis, args); // gunakan disini
3030
}, ms);
3131
};
3232

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
importance: 5
1+
nilai penting: 5
22

33
---
44

5-
# Delaying decorator
5+
# Dekorator penunda
66

7-
Create a decorator `delay(f, ms)` that delays each call of `f` by `ms` milliseconds.
7+
Buatlah sebuah dekorator `delay(f, ms)` yang menunda setiap pemanggilan dari `f` selama `ms` milidetik.
88

9-
For instance:
9+
Contoh:
1010

1111
```js
1212
function f(x) {
@@ -17,10 +17,10 @@ function f(x) {
1717
let f1000 = delay(f, 1000);
1818
let f1500 = delay(f, 1500);
1919

20-
f1000("test"); // shows "test" after 1000ms
21-
f1500("test"); // shows "test" after 1500ms
20+
f1000("test"); // tampilkan "test" setelah 1000ms
21+
f1500("test"); // tampilkan "test" setelah 1500ms
2222
```
2323

24-
In other words, `delay(f, ms)` returns a "delayed by `ms`" variant of `f`.
24+
Dengan kata lain, `delay(f, ms)` mengembalikan sebuah "varian dari `f` yang telah ditunda selama `ms`".
2525

26-
In the code above, `f` is a function of a single argument, but your solution should pass all arguments and the context `this`.
26+
Didalam kode diatas, `f` adalah sebuah fungsi dari sebuah argumen tunggal, tapi solusimu harus bisa melewati seluruh argumen dan konteks dari `this`.

1-js/06-advanced-functions/09-call-apply-decorators/03-debounce/_js.view/test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ describe('debounce', function () {
2222
const debounced = debounce(f, 1000);
2323

2424
debounced('a');
25-
setTimeout(() => debounced('b'), 200); // ignored (too early)
26-
setTimeout(() => debounced('c'), 500); // runs (1000 ms passed)
25+
setTimeout(() => debounced('b'), 200); // diabaikan (terlalu dini)
26+
setTimeout(() => debounced('c'), 500); // dijalankan (1000 ms telah berlalu)
2727
this.clock.tick(1000);
2828

2929
assert(f.notCalled, 'not called after 1000ms');

1-js/06-advanced-functions/09-call-apply-decorators/03-debounce/solution.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ function debounce(func, ms) {
99

1010
```
1111

12-
A call to `debounce` returns a wrapper. When called, it schedules the original function call after given `ms` and cancels the previous such timeout.
12+
Pemanggilan kepada `debounce` mengembalikan sebuah pembungkus. Ketika dipanggil, `debounce` akan menunggu lalu memanggil fungsi aslinya setelah `ms` milidetik dan membatal kan timeout sebelumnya.
1313

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,51 @@
1-
importance: 5
1+
nilai penting: 5
22

33
---
44

55
# Debounce decorator
66

7-
The result of `debounce(f, ms)` decorator is a wrapper that suspends calls to `f` until there's `ms` milliseconds of inactivity (no calls, "cooldown period"), then invokes `f` once with the latest arguments.
7+
Hasil dari dekorator `debounce(f, ms)` adalah sebuah pembungkus yang menghentikan pemanggilan `f` selama `ms` milidetik dari ketidakaktifan (tidak ada pemanggilan, "masa menunggu"), lalu memanggil `f` sekali dengan argumen terakhir.
88

9-
In other words, `debounce` is like a secretary that accepts "phone calls", and waits until there's `ms` milliseconds of being quiet. And only then it transfers the latest call information to "the boss" (calls the actual `f`).
9+
Dengan kata lain, `debounce` seperti seorang sekertaris yang menerima "telefon", dan menunggu selama `ms` milidetik dari ketidakaktifan. Dan lalu menyampaikan pemanggilan terakhir kepada "boss" (melakukan pemanggilan `f`).
1010

11-
For instance, we had a function `f` and replaced it with `f = debounce(f, 1000)`.
11+
Contoh, jika kita mempunyai sebuah fungsi `f` dan lalu memasukan `f = debounce(f, 1000)`.
1212

13-
Then if the wrapped function is called at 0ms, 200ms and 500ms, and then there are no calls, then the actual `f` will be only called once, at 1500ms. That is: after the cooldown period of 1000ms from the last call.
13+
Maka jika fungsi pembungkus dipanggil pada 0ms, 200ms, dan 500ms, dan lalu tidak ada pemanggilan lainnya, maka fungsi `f` akan dipanggil sekali, pada 1500ms. Itulah: setelah beberapa saat fungsi tidak dipanggil maka fungsinya akan benar-benar dipanggil dengan rentang waktu 1000ms setelah pemanggilan terakhir.
1414

1515
![](debounce.svg)
1616

17-
...And it will get the arguments of the very last call, other calls are ignored.
17+
...Dan itu akan mendapatkan argumen dari pemanggilan yang paling terakhir, pemanggilan lainnya akan diabaikan.
1818

19-
Here's the code for it (uses the debounce decorator from the [Lodash library](https://lodash.com/docs/4.17.15#debounce):
19+
Ini adalah kodenya (digunakan untuk dekorator debounce dari [Lodash library](https://lodash.com/docs/4.17.15#debounce)):
2020

2121
```js
2222
let f = _.debounce(alert, 1000);
2323

2424
f("a");
2525
setTimeout( () => f("b"), 200);
2626
setTimeout( () => f("c"), 500);
27-
// debounced function waits 1000ms after the last call and then runs: alert("c")
27+
// fungsi debounce menunggu 1000ms setelah pemanggilan terakhir dan lalu menjalankan: alert("c")
2828
```
2929

30-
Now a practical example. Let's say, the user types something, and we'd like to send a request to the server when the input is finished.
30+
Sekarang contoh yang lebih praktikal. Katakan, penggunakan mengetik sesuatu, dan kita ingin mengirim request kepada server ketika pengguna telah selesai mengetik.
3131

32-
There's no point in sending the request for every character typed. Instead we'd like to wait, and then process the whole result.
32+
Pada hal ini, sangat tidak berguna untuk mengirim request kepada server untuk setiap huruf yang diketik. Lagipula kita ingin menunggu, dan lalu memproses hasil ketikan pengguna.
3333

34-
In a web-browser, we can setup an event handler -- a function that's called on every change of an input field. Normally, an event handler is called very often, for every typed key. But if we `debounce` it by 1000ms, then it will be only called once, after 1000ms after the last input.
34+
Didalam peramban, kita bisa menyetel sebuah event handler(penangan event) -- sebuah fungsi yang dipanggi untuk setiap perubahan pada kotak inputan, sebuah penangan event dipanggil sangat sering untuk setiap huruf yang diketik. Tapi jika kita ingin men`debounce`nya selama 1000ms, maka fungsinya akan dipanggil sekali, 1000ms setelah penginputan huruf terakhir.
3535

3636
```online
3737
38-
In this live example, the handler puts the result into a box below, try it:
38+
Didalam contoh ini, handlernya memasukan hasilnya kedalam kotak dibawah, cobalah:
3939
4040
[iframe border=1 src="debounce" height=200]
4141
42-
See? The second input calls the debounced function, so its content is processed after 1000ms from the last input.
42+
Lihat? inputan kedua memanggil fungsi debounce, jadi kontennya diproses setelah 1000ms dari inputan terakhir.
4343
```
4444

45-
So, `debounce` is a great way to process a sequence of events: be it a sequence of key presses, mouse movements or something else.
45+
Jadi, `debounce` adalah cara terbaik untuk memproses event yang terjadi berurutan: bisa tombol yang dipencet berulang-ulang, pergerakan mouse atau lainnya.
4646

47-
It waits the given time after the last call, and then runs its function, that can process the result.
47+
Fungsinya akan menunggu hingga pemanggilan terakhir, dan lalu menjalankan fungsi aslinya, lalu hasilnya akan diolah.
4848

49-
The task is to implement `debounce` decorator.
49+
Tugasnya adalah untuk mengimplementasikan dekorator `debounce`.
5050

51-
Hint: that's just a few lines if you think about it :)
51+
Petunjuk: jika kamu perhatikan, perubahan fungsinya hanya dengan menambahkan beberapa baris :)

1-js/06-advanced-functions/09-call-apply-decorators/04-throttle/_js.view/solution.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,23 @@ function throttle(func, ms) {
77
function wrapper() {
88

99
if (isThrottled) {
10-
// memo last arguments to call after the cooldown
10+
// mengingat argumen terakhir untuk diingat setelah kondisi tidak aktif
1111
savedArgs = arguments;
1212
savedThis = this;
1313
return;
1414
}
1515

16-
// otherwise go to cooldown state
16+
// sebaliknya, masuk kedalam kondisi tidak aktif
1717
func.apply(this, arguments);
1818

1919
isThrottled = true;
2020

21-
// plan to reset isThrottled after the delay
21+
// mereset isThrotthled setelah penundaan
2222
setTimeout(function() {
2323
isThrottled = false;
2424
if (savedArgs) {
25-
// if there were calls, savedThis/savedArgs have the last one
26-
// recursive call runs the function and sets cooldown again
25+
// jika terdapat sebuah pemanggilan, savedThis/savedArgs harusnya memiliki nilai terakhir
26+
// pemanggilan rekursif menjalankan fungsinya dan menyetel ulang kondisi tidak aktifnya.
2727
wrapper.apply(savedThis, savedArgs);
2828
savedArgs = savedThis = null;
2929
}

0 commit comments

Comments
 (0)