Skip to content

Commit e2197df

Browse files
authored
Merge pull request #162 from pasor1/article/17-arrow-functions-basics
Arrow functions, the basics
2 parents 157d82f + ac0406c commit e2197df

File tree

2 files changed

+34
-32
lines changed

2 files changed

+34
-32
lines changed

1-js/02-first-steps/17-arrow-functions-basics/1-rewrite-arrow/task.md

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

2-
# Riscrivi con funzioni freccia
2+
# Riscrivi usando le arrow functions
33

4-
Rimpiazza le espressioni di funzione con funzioni freccia:
4+
Sostituisci le function expressions con arrow functions:
55

66
```js run
77
function ask(question, yes, no) {
Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
1-
# Arrow functions, the basics
1+
# Arrow functions, le basi
22

3-
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.
44

5-
It's called "arrow functions", because it looks like this:
5+
E' chiamata "arrow functions", perché si presenta in questo modo:
66

77
```js
88
let func = (arg1, arg2, ...argN) => expression
99
```
1010

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.
1212

13-
In other words, it's the shorter version of:
13+
In altre parole è una versione abbreviata di:
1414

1515
```js
1616
let func = function(arg1, arg2, ...argN) {
1717
return expression;
1818
};
1919
```
2020

21-
Let's see a concrete example:
21+
Vediamo un esempio concreto:
2222

2323
```js run
2424
let sum = (a, b) => a + b;
2525

26-
/* This arrow function is a shorter form of:
26+
/* Questa arrow function è una versione abbreviata di:
2727
2828
let sum = function(a, b) {
2929
return a + b;
@@ -33,32 +33,32 @@ let sum = function(a, b) {
3333
alert( sum(1, 2) ); // 3
3434
```
3535

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.
3737

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.
3939

40-
For example:
40+
Ad esempio:
4141

4242
```js run
4343
*!*
4444
let double = 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 }
4646
*/!*
4747

4848
alert( double(3) ); // 6
4949
```
5050

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):
5252

5353
```js run
5454
let sayHi = () => alert("Hello!");
5555
5656
sayHi();
5757
```
5858

59-
Arrow functions can be used in the same way as Function Expressions.
59+
Le arrow functions possono essere usate allo stesso modo delle Function Expressions.
6060

61-
For instance, to dynamically create a function:
61+
Ad esempio, per creare dinamicamente una funzione:
6262

6363
```js run
6464
let age = prompt("What is your age?", 18);
@@ -70,42 +70,44 @@ let welcome = (age < 18) ?
7070
welcome();
7171
```
7272

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.
7474
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.
7676
77-
## Multiline arrow functions
77+
## Arrow functions su più linee
7878
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.
8080

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.
8282
83-
Like this:
83+
In questo modo:
8484
8585
```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
8787
let result = a + b;
8888
*!*
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"
9090
*/!*
9191
};
9292
9393
alert( sum(1, 2) ); // 3
9494
```
9595
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ù..."
9897
99-
Arrow functions have other interesting features.
98+
Qui abbiamo presentato le arrow functions in breve, ma questo non è tutto!
10099
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.
102101
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.
104105
```
105106
106107
## Summary
107108
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:
109110
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
113+
`return` affinché sia ritornato qualcosa.

0 commit comments

Comments
 (0)