Skip to content

Commit 35ccebe

Browse files
authored
Merge pull request #204 from pasor1/article/09-call-apply-decorators
Decorators and forwarding, call/apply
2 parents 577d580 + b90faf6 commit 35ccebe

File tree

16 files changed

+253
-249
lines changed

16 files changed

+253
-249
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+
// usiamo ...args invece di arguments per memorizzare un vero array in 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+
// il tuo codice
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+
Il wrapper restituito da `spy (f)` dovrebbe memorizzare tutti gli argomenti e quindi usare `f.apply` per inoltrare la chiamata.

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
@@ -2,17 +2,17 @@ importance: 5
22

33
---
44

5-
# Spy decorator
5+
# decorator spia
66

7-
Create a decorator `spy(func)` that should return a wrapper that saves all calls to function in its `calls` property.
7+
Crea un decorator `spy(func)` che restituisca un wrapper che salva tutte le chiamate alla funzione nella sua proprietà `calls`.
88

9-
Every call is saved as an array of arguments.
9+
Ogni chiamata viene salvata come un array di argomenti.
1010

11-
For instance:
11+
Ad esempio:
1212

1313
```js
1414
function work(a, b) {
15-
alert( a + b ); // work is an arbitrary function or method
15+
alert( a + b ); // work è una funzione o un metodo arbitrario
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+
P.S. Questo decorator a volte è utile per fare *unit-testing*. La sua forma avanzata è `sinon.spy` nella libreria [Sinon.JS](http://sinonjs.org/).

1-js/06-advanced-functions/09-call-apply-decorators/02-delay/_js.view/test.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
describe("delay", function() {
2-
before(function() {
1+
describe("delay", function () {
2+
before(function () {
33
this.clock = sinon.useFakeTimers();
44
});
55

6-
after(function() {
6+
after(function () {
77
this.clock.restore();
88
});
99

10-
it("calls the function after the specified timeout", function() {
10+
it("chiama la funzione dopo il timeout specificato", function () {
1111
let start = Date.now();
1212

1313
function f(x) {
@@ -18,13 +18,13 @@ describe("delay", function() {
1818
let f1000 = delay(f, 1000);
1919
f1000("test");
2020
this.clock.tick(2000);
21-
assert(f.calledOnce, 'calledOnce check fails');
21+
assert(f.calledOnce, 'controllo di calledOnce fallito');
2222
});
2323

24-
it("passes arguments and this", function() {
24+
it("passa argomenti e this", function () {
2525
let start = Date.now();
2626
let user = {
27-
sayHi: function(phrase, who) {
27+
sayHi: function (phrase, who) {
2828
assert.equal(this, user);
2929
assert.equal(phrase, "Hello");
3030
assert.equal(who, "John");
@@ -41,6 +41,6 @@ describe("delay", function() {
4141

4242
this.clock.tick(2000);
4343

44-
assert(spy.calledOnce, 'calledOnce check failed');
44+
assert(spy.calledOnce, 'controllo di calledOnce fallito');
4545
});
4646
});

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+
La soluzione:
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"); // mostra "test" dopo 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+
Qui, nota come viene utilizzata un arrow function. come sappiamo le arrow functions non hanno un proprio `this` `arguments`, quindi `f.apply(this, arguments)` prende `this` e `arguments` dal wrapper.
1818

19-
If we pass a regular function, `setTimeout` would call it without arguments and `this=window` (assuming we're in the browser).
19+
Se passassimo una funzione regolare, `setTimeout` la chiamerebbe senza argomenti e` this = window` (supponendo essere in un browser).
2020

21-
We still can pass the right `this` by using an intermediate variable, but that's a little bit more cumbersome:
21+
Possiamo anche passare il `this` corretto usando una variabile intermedia, ma è un po' più complicato:
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; // memorizzalo in una variabile intermedia
2828
setTimeout(function() {
29-
f.apply(savedThis, args); // use it here
29+
f.apply(savedThis, args); // usalo qui
3030
}, ms);
3131
};
3232

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,25 @@ importance: 5
22

33
---
44

5-
# Delaying decorator
5+
# decorator ritardante
66

7-
Create a decorator `delay(f, ms)` that delays each call of `f` by `ms` milliseconds.
7+
Crea il decorator `delay(f, ms)` che ritarda ogni chiamata ad `f` di `ms` millisecondi.
88

9-
For instance:
9+
Ad esempio:
1010

1111
```js
1212
function f(x) {
1313
alert(x);
1414
}
1515

16-
// create wrappers
16+
// crea i wrappers
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"); // visualizza "test" dopo 1000ms
21+
f1500("test"); // visualizza "test" dopo 1500ms
2222
```
2323

24-
In other words, `delay(f, ms)` returns a "delayed by `ms`" variant of `f`.
24+
In altre parole, `delay(f, ms)` ritorna una variante di `f` ritardata di `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+
Nel codice sopra, `f` è una funzione con un solo argomento, ma la tua soluzione potrebbe passare molti argomenti ed il contesto `this`.

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

Lines changed: 3 additions & 3 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); // ignorato (tropo presto)
26+
setTimeout(() => debounced('c'), 500); // eseguito (1000 ms passati)
2727
this.clock.tick(1000);
2828

2929
assert(f.notCalled, 'not called after 1000ms');
@@ -44,5 +44,5 @@ describe('debounce', function () {
4444
obj.f('test');
4545
this.clock.tick(5000);
4646
});
47-
47+
4848
});
Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
<!doctype html>
22
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.js"></script>
33

4-
Function <code>handler</code> is called on this input:
4+
Function <code>handler</code> viene chiamata su questo input:
55
<br>
66
<input id="input1" placeholder="type here">
77

88
<p>
99

10-
Debounced function <code>debounce(handler, 1000)</code> is called on this input:
11-
<br>
12-
<input id="input2" placeholder="type here">
10+
Debounced function <code>debounce(handler, 1000)</code> viene chiamata su questo input:
11+
<br>
12+
<input id="input2" placeholder="type here">
1313

1414
<p>
15-
<button id="result">The <code>handler</code> puts the result here</button>
15+
<button id="result">L' <code>handler</code> mette il risultato qui</button>
1616

17-
<script>
18-
function handler(event) {
19-
result.innerHTML = event.target.value;
20-
}
17+
<script>
18+
function handler(event) {
19+
result.innerHTML = event.target.value;
20+
}
2121

22-
input1.oninput = handler;
23-
input2.oninput = _.debounce(handler, 1000);
24-
</script>
22+
input1.oninput = handler;
23+
input2.oninput = _.debounce(handler, 1000);
24+
</script>

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+
Una chiamata a `debounce` restituisce un wrapper. Quando viene chiamato, pianifica la chiamata della funzione originale dopo tot `ms` e annulla il precedente timeout.
1313

0 commit comments

Comments
 (0)