You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
There's another very simple and concise syntax for creating functions, that's often better than Function Expressions.
3
+
Esiste un'altra sintassi molto semplice e concisa per creare funzioni e che spesso è migliore delle Function Expressions.
4
4
5
-
It's called "arrow functions", because it looks like this:
5
+
E' chiamata "arrow functions", perché si presenta in questo modo:
6
6
7
7
```js
8
8
letfunc= (arg1, arg2, ...argN) => expression
9
9
```
10
10
11
-
...This creates a function `func`that accepts arguments `arg1..argN`, then evaluates the `expression`on the right side with their use and returns its result.
11
+
Questo codice crea una funzione `func`che accetta gli argomenti `arg1..argN` e li utilizza per valutare `expression`e restituirne il risultato.
12
12
13
-
In other words, it's the shorter version of:
13
+
In altre parole è una versione abbreviata di:
14
14
15
15
```js
16
16
letfunc=function(arg1, arg2, ...argN) {
17
17
return expression;
18
18
};
19
19
```
20
20
21
-
Let's see a concrete example:
21
+
Vediamo un esempio concreto:
22
22
23
23
```js run
24
24
letsum= (a, b) => a + b;
25
25
26
-
/*This arrow function is a shorter form of:
26
+
/*Questa arrow function è una versione abbreviata di:
27
27
28
28
let sum = function(a, b) {
29
29
return a + b;
@@ -33,32 +33,32 @@ let sum = function(a, b) {
33
33
alert( sum(1, 2) ); // 3
34
34
```
35
35
36
-
As you can, see `(a, b) => a + b`means a function that accepts two arguments named `a`and`b`. Upon the execution, it evaluates the expression `a + b`and returns the result.
36
+
Come puoi vedere `(a, b) => a + b`rappresenta una funzione che accetta due argomenti `a`e`b`. Al momento dell'esecuzione, questa valuta l'espressione `a + b`e restituisce il risultato.
37
37
38
-
-If we have only one argument, then parentheses around parameters can be omitted, making that even shorter.
38
+
-Se abbiamo un solo argomento, le parentesi che racchiudono gli argomenti possono essere omesse, abbreviando ulteriormente il codice.
39
39
40
-
For example:
40
+
Ad esempio:
41
41
42
42
```js run
43
43
*!*
44
44
letdouble=n=> n *2;
45
-
//roughly the same as: let double = function(n) { return n * 2 }
45
+
//più o meno lo steso di: let double = function(n) { return n * 2 }
46
46
*/!*
47
47
48
48
alert( double(3) ); // 6
49
49
```
50
50
51
-
-If there are no arguments, parentheses will be empty (but they should be present):
51
+
-Se non ci sono argomenti, le parentesi saranno vuote (ma devono essere presenti):
52
52
53
53
```js run
54
54
let sayHi = () => alert("Hello!");
55
55
56
56
sayHi();
57
57
```
58
58
59
-
Arrow functions can be used in the same way asFunction Expressions.
59
+
Le arrow functions possono essere usate allo stesso modo delleFunction Expressions.
60
60
61
-
For instance, to dynamically create a function:
61
+
Ad esempio, per creare dinamicamente una funzione:
62
62
63
63
```js run
64
64
let age = prompt("What is your age?", 18);
@@ -70,42 +70,44 @@ let welcome = (age < 18) ?
70
70
welcome();
71
71
```
72
72
73
-
Arrow functions may appear unfamiliar and not very readable at first, but that quickly changes as the eyes get used to the structure.
73
+
Le arrow functions possono apparire poco familiari e leggibili all'inizio, ma ciò cambia rapidamente man mano che gli occhi si abitueranno alla struttura.
74
74
75
-
They are very convenient for simple one-line actions, when we're just too lazy to write many words.
75
+
Esse sono molto comode per semplici azioni su una riga, se siamo troppo pigri per scrivere più parole.
76
76
77
-
## Multiline arrow functions
77
+
## Arrow functions su più linee
78
78
79
-
The examples above took arguments from the left of `=>` and evaluated the right-side expression with them.
79
+
Gli esempi precedenti hanno preso argomenti alla sinistra di "=>" e con essi hanno valutato l'espressione a destra.
80
80
81
-
Sometimes we need something a little bit more complex, like multiple expressions or statements. It is also possible, but we should enclose them in curly braces. Then use a normal `return` within them.
81
+
A volte abbiamo bisogno di qualcosa di un po' più complesso, come espressioni o dichiarazioni multiple. Anche questo è possibile, ma dovremo racchiuderle tra parentesi graffe ed usare un normale return.
82
82
83
-
Like this:
83
+
In questo modo:
84
84
85
85
```js run
86
-
let sum = (a, b) => { // the curly brace opens a multiline function
86
+
let sum = (a, b) => { // le parentesi graffe aprono una funzione multilinea
87
87
let result = a + b;
88
88
*!*
89
-
return result; // if we use curly braces, then we need an explicit "return"
89
+
return result; // se usiamo le parentesi graffe abbiamo bisogno di un esplicito "return"
90
90
*/!*
91
91
};
92
92
93
93
alert( sum(1, 2) ); // 3
94
94
```
95
95
96
-
```smart header="More to come"
97
-
Here we praised arrow functions for brevity. But that's not all!
96
+
```smart header="Molto di più..."
98
97
99
-
Arrow functions have other interesting features.
98
+
Qui abbiamo presentato le arrow functions in breve, ma questo non è tutto!
100
99
101
-
To study them in-depth, we first need to get to know some other aspects of JavaScript, so we'll return to arrow functions later in the chapter <info:arrow-functions>.
100
+
Le arrow functions possiedono altre interessanti caratteristiche.
102
101
103
-
For now, we can already use arrow functions for one-line actions and callbacks.
102
+
Per studiarle approfonditamente dobbiamo prima conoscere qualche altro aspetto di JavaScript, quindi torneremo alle arrow functions più avanti, nel capitolo <info:arrow-functions>.
103
+
104
+
Per ora possiamo già utilizzarle per azioni su una riga sola e per callbacks.
104
105
```
105
106
106
107
## Summary
107
108
108
-
Arrow functions are handy for one-liners. They come in two flavors:
109
+
Le arrow functions sono utili per azioni su una riga sola. Possono essere scritte in due modi:
109
110
110
-
1. Without curly braces: `(...args) => expression` -- the right side is an expression: the function evaluates it and returns the result.
111
-
2. With curly braces: `(...args) => { body }` -- brackets allow us to write multiple statements inside the function, but we need an explicit `return` to return something.
111
+
1. Senza parentesi graffe: `(...args) => expression` -- la parte destra è un'espressione: la funzione la valuta e restituisce il risultato.
112
+
2. Con parentesi graffe:`(...args) => { body }`-- le parentesi ci permettono di scrivere comandi multipli all'interno della funzione, ma abbiamo bisogno di dichiarare esplicitamente
0 commit comments