Skip to content

Commit c864840

Browse files
authored
Merge pull request #229 from pasor1/article/10-bind
Function binding
2 parents d014c6f + 7aed099 commit c864840

File tree

11 files changed

+111
-114
lines changed

11 files changed

+111
-114
lines changed

1-js/06-advanced-functions/10-bind/2-write-to-object-after-bind/solution.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
The answer: `null`.
1+
Risposta: `null`.
22

33

44
```js run
@@ -13,6 +13,6 @@ let user = {
1313
user.g();
1414
```
1515

16-
The context of a bound function is hard-fixed. There's just no way to further change it.
16+
Il contesto di una funzione associata è fisso. Non esiste alcun modo di cambiarlo successivamente.
1717

18-
So even while we run `user.g()`, the original function is called with `this=null`.
18+
Quindi, anche se eseguiamo `user.g()`, la funzione originale verrà chiamata con `this=null`.

1-js/06-advanced-functions/10-bind/2-write-to-object-after-bind/task.md

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

33
---
44

5-
# Bound function as a method
5+
# Funzione associata come metodo
66

7-
What will be the output?
7+
Quale sarà l'output?
88

99
```js
1010
function f() {

1-js/06-advanced-functions/10-bind/3-second-bind/solution.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
The answer: **John**.
1+
Risposta: **John**.
22

33
```js run no-beautify
44
function f() {
@@ -10,6 +10,6 @@ f = f.bind( {name: "John"} ).bind( {name: "Pete"} );
1010
f(); // John
1111
```
1212

13-
The exotic [bound function](https://tc39.github.io/ecma262/#sec-bound-function-exotic-objects) object returned by `f.bind(...)` remembers the context (and arguments if provided) only at creation time.
13+
L' *exotic object* [bound function](https://tc39.github.io/ecma262/#sec-bound-function-exotic-objects) restituito da `f.bind(...)` memorizza il contesto (e gli argomenti, se forniti) solo in fase di creazione.
1414

15-
A function cannot be re-bound.
15+
Una funzione non può essere riassegnata.

1-js/06-advanced-functions/10-bind/3-second-bind/task.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ importance: 5
22

33
---
44

5-
# Second bind
5+
# Secondo bind
66

7-
Can we change `this` by additional binding?
7+
Possiamo cambiare `this` con una associazione addizionale?
88

9-
What will be the output?
9+
Quale sarà l'output?
1010

1111
```js no-beautify
1212
function f() {
@@ -17,4 +17,3 @@ f = f.bind( {name: "John"} ).bind( {name: "Ann" } );
1717

1818
f();
1919
```
20-
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
The answer: `undefined`.
1+
Risposta: `undefined`.
22

3-
The result of `bind` is another object. It does not have the `test` property.
3+
Il risultato di `bind` è un altro oggetto, che non ha la proprietà `test`.
44

1-js/06-advanced-functions/10-bind/4-function-property-after-bind/task.md

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

33
---
44

5-
# Function property after bind
5+
# Proprietà della funzione dopo il bind
66

7-
There's a value in the property of a function. Will it change after `bind`? Why, or why not?
7+
C'è un valore nella proprietà di una funzione. Cambierà dopo `bind`? Perché, o perché no?
88

99
```js run
1010
function sayHi() {
@@ -17,7 +17,7 @@ let bound = sayHi.bind({
1717
name: "John"
1818
});
1919

20-
alert( bound.test ); // what will be the output? why?
20+
alert( bound.test ); // quale sarà l'output? Perché?
2121
*/!*
2222
```
2323

1-js/06-advanced-functions/10-bind/5-question-use-bind/solution.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11

2-
The error occurs because `ask` gets functions `loginOk/loginFail` without the object.
2+
L'errore si verifica perché `askPassword` riceve le funzioni `loginOk/loginFail` senza l'oggetto.
33

4-
When it calls them, they naturally assume `this=undefined`.
4+
Quando le chiamiamo, naturalmente assumono `this=undefined`.
55

6-
Let's `bind` the context:
6+
Usiamo `bind` per associare il contesto:
77

88
```js run
99
function askPassword(ok, fail) {
@@ -30,14 +30,14 @@ askPassword(user.loginOk.bind(user), user.loginFail.bind(user));
3030
*/!*
3131
```
3232

33-
Now it works.
33+
Ora funziona.
3434

35-
An alternative solution could be:
35+
Una soluzione alternativa potrebbe essere:
3636
```js
3737
//...
3838
askPassword(() => user.loginOk(), () => user.loginFail());
3939
```
4040

41-
Usually that also works and looks good.
41+
Di solito anche questo funziona e appare come una buona soluzione.
4242

43-
It's a bit less reliable though in more complex situations where `user` variable might change *after* `askPassword` is called, but *before* the visitor answers and calls `() => user.loginOk()`.
43+
Tuttavia è un po' meno affidabile in situazioni più complesse, in cui la variabile `user` potrebbe cambiare *dopo* la chiamata di `askPassword`, ma *prima* che il visitatore risponda e venga chiamata `() => user.loginOk()`.

1-js/06-advanced-functions/10-bind/5-question-use-bind/task.md

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

33
---
44

5-
# Fix a function that loses "this"
5+
# Correggi una funzione che ha perso "this"
66

7-
The call to `askPassword()` in the code below should check the password and then call `user.loginOk/loginFail` depending on the answer.
7+
La chiamata di `askPassword()` nel codice sottostante dovrebbe controllare la password e quindi chiamare `user.loginOk/loginFail` a seconda della risposta.
88

9-
But it leads to an error. Why?
9+
Ma porta a un errore. Perché?
1010

11-
Fix the highlighted line for everything to start working right (other lines are not to be changed).
11+
Correggi la riga evidenziata affinché tutto funzioni correttamente (le altre righe non devono essere modificate).
1212

1313
```js run
1414
function askPassword(ok, fail) {

1-js/06-advanced-functions/10-bind/6-ask-partial/solution.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11

22

3-
1. Either use a wrapper function, an arrow to be concise:
3+
1. Puoi sia utilizzare una funzione wrapper, che una arrow per essere concisi:
44

55
```js
66
askPassword(() => user.login(true), () => user.login(false));
77
```
88

9-
Now it gets `user` from outer variables and runs it the normal way.
9+
Ora riceve `user` dalla variabile esterna ed esegue la funzione in maniera corretta.
1010

11-
2. Or create a partial function from `user.login` that uses `user` as the context and has the correct first argument:
11+
2. Oppure creare una funzione parziale da `user.login` che utilizzi `user` come contesto ed abbia il giusto primo argomento:
1212

1313

1414
```js

1-js/06-advanced-functions/10-bind/6-ask-partial/task.md

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

33
---
44

5-
# Partial application for login
5+
# Applicazione parziale per login
66

7-
The task is a little more complex variant of <info:task/question-use-bind>.
7+
Il compito è una variante leggermente più complessa di <info:task/question-use-bind>.
88

9-
The `user` object was modified. Now instead of two functions `loginOk/loginFail`, it has a single function `user.login(true/false)`.
9+
L'oggetto `user` è stato modificato. Ora al posto delle due funzioni `loginOk/loginFail`, ha una sola funzione `user.login(true/false)`.
1010

11-
What should we pass `askPassword` in the code below, so that it calls `user.login(true)` as `ok` and `user.login(false)` as `fail`?
11+
Cosa dovremmo passare a `askPassword` nel codice qui sotto, in modo che chiami `user.login(true)` come `ok` e `user.login(false)` come `fail`?
1212

1313
```js
1414
function askPassword(ok, fail) {
@@ -30,5 +30,4 @@ askPassword(?, ?); // ?
3030
*/!*
3131
```
3232
33-
Your changes should only modify the highlighted fragment.
34-
33+
Le tue modifiche dovrebbero solo interessare la porzione di codice evidenziata.

0 commit comments

Comments
 (0)