Skip to content

Commit 1d1858e

Browse files
authored
Merge pull request #233 from longo-andrea/article/revisited-arrow-functions
Arrow functions revisited
2 parents cd721ea + f017688 commit 1d1858e

File tree

1 file changed

+35
-35
lines changed
  • 1-js/06-advanced-functions/12-arrow-functions

1 file changed

+35
-35
lines changed
Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
1-
# Arrow functions revisited
1+
# Arrow functions rivisitate
22

3-
Let's revisit arrow functions.
3+
Rivediamo l'argomento delle arrow functions.
44

5-
Arrow functions are not just a "shorthand" for writing small stuff. They have some very specific and useful features.
5+
Le arrow functions non sono semplicemente una "scorciatoia" per scrivere codice più breve. Infatti queste possiedono delle caratteristiche aggiuntive piuttosto utili.
66

7-
JavaScript is full of situations where we need to write a small function that's executed somewhere else.
7+
Usando JavaScript ci troviamo spesso a dover scrivere delle funzioni molto semplici, che verranno però eseguite in un diverso punto del codice.
88

9-
For instance:
9+
Ad esemmpio:
1010

11-
- `arr.forEach(func)` -- `func` is executed by `forEach` for every array item.
12-
- `setTimeout(func)` -- `func` is executed by the built-in scheduler.
13-
- ...there are more.
11+
- `arr.forEach(func)` -- `func` viene eseguita da `forEach` per ogni elemento dell'array.
12+
- `setTimeout(func)` -- `func` viene eseguita dallo scheduler integrato.
13+
- ...ed esistono molti altri casi.
1414

15-
It's in the very spirit of JavaScript to create a function and pass it somewhere.
15+
È nel vero spirito di JavaScript poter creare una funzione in un punto e passarla in qualsiasi altra parte del codice.
1616

17-
And in such functions we usually don't want to leave the current context. That's where arrow functions come in handy.
17+
E in questo tipo di funzioni, generalmente, non vorremmo perdere il riferimento al context (il contesto in cui la funzione è stata definita). Queste sono le situazioni in cui le arrow functions ci vengono in soccorso.
1818

19-
## Arrow functions have no "this"
19+
## Le arrow functions non possiedono un "this"
2020

21-
As we remember from the chapter <info:object-methods>, arrow functions do not have `this`. If `this` is accessed, it is taken from the outside.
21+
Come già abbiamo studiato nel capitolo <info:object-methods>, le arrow functions non possiedono un `this`. Infatti, il valore di `this`, viene preso dal contesto esterno.
2222

23-
For instance, we can use it to iterate inside an object method:
23+
Ad esempio, possiamo utilizzarlo per le iterazioni all'interno del un metodo di un oggetto:
2424

2525
```js run
2626
let group = {
@@ -39,9 +39,9 @@ let group = {
3939
group.showList();
4040
```
4141

42-
Here in `forEach`, the arrow function is used, so `this.title` in it is exactly the same as in the outer method `showList`. That is: `group.title`.
42+
Qui, all'interno del `forEach`, viene utilizzata una arrow function, quindi il valore di `this.title` è esattamente lo stesso che troviamo nel metodo esterno `showList`. Cioè: `group.title`.
4343

44-
If we used a "regular" function, there would be an error:
44+
Se avessimo utilizzato una funzione "regolare", avremmo ottenuto un errore:
4545

4646
```js run
4747
let group = {
@@ -61,28 +61,28 @@ let group = {
6161
group.showList();
6262
```
6363

64-
The error occurs because `forEach` runs functions with `this=undefined` by default, so the attempt to access `undefined.title` is made.
64+
L'errore viene generato perché `forEach` esegue le funzioni con `this=undefined` di default, quindi il codice equivale a: `undefined.title`.
6565

66-
That doesn't affect arrow functions, because they just don't have `this`.
66+
Questo non si verifica con le arrow functions, poiché queste non possiedono un `this`.
6767

68-
```warn header="Arrow functions can't run with `new`"
69-
Not having `this` naturally means another limitation: arrow functions can't be used as constructors. They can't be called with `new`.
68+
```warn header="Arrow functions non funzionano con `new`"
69+
Il fatto di non possedere un `this` porta ad una naturale limitazione: le arrow functions non possono essere utilizzate come costruttori. Non possono essere invocate con la keyword `new`.
7070
```
7171
7272
```smart header="Arrow functions VS bind"
73-
There's a subtle difference between an arrow function `=>` and a regular function called with `.bind(this)`:
73+
Esiste una sottile differenza tra l'utilizzo di una arrow function `=>` ed una funzione regolare invocata con `.bind(this)`:
7474
75-
- `.bind(this)` creates a "bound version" of the function.
76-
- The arrow `=>` doesn't create any binding. The function simply doesn't have `this`. The lookup of `this` is made exactly the same way as a regular variable search: in the outer lexical environment.
75+
- `.bind(this)` crea una versione "bounded" (delimitata) della funzione.
76+
- Le arrow functions, tramite `=>`, non creano alcun binding. La funzione semplicemente non avrà un `this`. La ricerca di `this` seguirà esattamente le stesse procedure della ricerca di una variabile: verrà cercata nel lexical environment esterno.
7777
```
7878

79-
## Arrows have no "arguments"
79+
## Le arrow functions non possiedono "arguments"
8080

81-
Arrow functions also have no `arguments` variable.
81+
Le arrow functions non possiedo la varibile `arguments`.
8282

83-
That's great for decorators, when we need to forward a call with the current `this` and `arguments`.
83+
Questo è fantastico per i decorators, in cui abbiamo biosogno di inoltrare una chiamata con il valori attuali di `this` e `arguments`.
8484

85-
For instance, `defer(f, ms)` gets a function and returns a wrapper around it that delays the call by `ms` milliseconds:
85+
Ad esempio, `defer(f, ms)` accetta una funzione come parametro e ritorna un wrapper della stessa, il quale ne ritarderà l'invocazione di `ms` millisecondi:
8686

8787
```js run
8888
function defer(f, ms) {
@@ -96,10 +96,10 @@ function sayHi(who) {
9696
}
9797

9898
let sayHiDeferred = defer(sayHi, 2000);
99-
sayHiDeferred("John"); // Hello, John after 2 seconds
99+
sayHiDeferred("John"); // Hello, John dopo 2 secondi
100100
```
101101

102-
The same without an arrow function would look like:
102+
La stessa cosa, senza una arrow function, sarebbe:
103103

104104
```js
105105
function defer(f, ms) {
@@ -112,15 +112,15 @@ function defer(f, ms) {
112112
}
113113
```
114114

115-
Here we had to create additional variables `args` and `ctx` so that the function inside `setTimeout` could take them.
115+
In questo caso abbiamo avuto bisogno di creare delle variabili aggiuntive come `args` e `ctx`, in modo che la funzione interna a `setTimeout` possa riceverle.
116116

117-
## Summary
117+
## Riepilogo
118118

119119
Arrow functions:
120120

121-
- Do not have `this`
122-
- Do not have `arguments`
123-
- Can't be called with `new`
124-
- They also don't have `super`, but we didn't study it yet. We will on the chapter <info:class-inheritance>
121+
- Non possiedono `this`
122+
- Non possiedono `arguments`
123+
- Non possono essere invocate con `new`
124+
- Non possiedono `super`, ma non lo abbiamo ancora studiato. Lo faremo nel capitolo <info:class-inheritance>
125125

126-
That's because they are meant for short pieces of code that do not have their own "context", but rather work in the current one. And they really shine in that use case.
126+
Questo perché sono pensate per piccole parti di codice che non possiedono un contesto proprio, ma piuttosto, lavorano nel contesto corrente. E sono veramente perfette per questi casi d'uso.

0 commit comments

Comments
 (0)