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
In the very first chapter about [variables](info:variables), we mentioned three ways of variable declaration:
4
+
Nei primi capitoli in cui abbiamo parlato di [variabili](info:variables), abbiamo menzionato tre diversi tipi di dichiarazione:
5
5
6
6
1.`let`
7
7
2.`const`
8
8
3.`var`
9
9
10
-
`let`and`const`behave exactly the same way in terms of Lexical Environments.
10
+
`let`e`const`si comportano esattamente allo stesso modo in termini di Lexical Environments.
11
11
12
-
But`var`is a very different beast, that originates from very old times. It's generally not used in modern scripts, but still lurks in the old ones.
12
+
Invece`var`è totalmente diverso, poiché deriva dalle prime versioni del linguaggio. Generalmente negli script più recenti non viene utilizzato, ma appare ancora in quelli più vecchi.
13
13
14
-
If you don't plan on meeting such scripts you may even skip this chapter or postpone it, but then there's a chance that it bites you later.
14
+
Se non avete intenzione di avere a che fare con gli script più vecchi, potete saltare questo capitolo o studiarlo in futuro, ma molto probabilmente prima o poi vi ci imbatterete.
15
15
16
-
From the first sight, `var`behaves similar to `let`. That is, declares a variable:
16
+
A prima vista, `var`si comporta in maniera analoga a `let`. Ad esempio:
17
17
18
18
```js run
19
19
functionsayHi() {
20
-
var phrase ="Hello"; //local variable, "var" instead of "let"
20
+
var phrase ="Hello"; //variabile locale, "var" piuttosto di "let"
21
21
22
22
alert(phrase); // Hello
23
23
}
24
24
25
25
sayHi();
26
26
27
-
alert(phrase); //Error, phrase is not defined
27
+
alert(phrase); //Errore, phrase non è definito
28
28
```
29
29
30
-
...But here are the differences.
30
+
...Abbiamo trovato una differenza.
31
31
32
-
## "var" has no block scope
32
+
## "var" non ha uno scope di blocco
33
33
34
-
`var` variables are either function-wide or global, they are visible through blocks.
34
+
Le variabili dichiarate tramite `var` possono essere: locali alla funzione oppure globali.
35
35
36
-
For instance:
36
+
Ad esempio:
37
37
38
38
```js
39
39
if (true) {
40
-
var test =true; //use "var" instead of "let"
40
+
var test =true; //utilizziamo "var" piuttosto di "let"
41
41
}
42
42
43
43
*!*
44
-
alert(test); //true, the variable lives after if
44
+
alert(test); //vero, la variabile vive dopo if
45
45
*/!*
46
46
```
47
47
48
-
If we used`let test`on the 2nd line, then it wouldn't be visible to`alert`. But`var`ignores code blocks, so we've got a global `test`.
48
+
Se avessimo utilizzato`let test`nella seconda riga, allora non sarebbe stata visibile ad`alert`. Ma`var`ignora i blocchi di codice, quindi `test` risulta essere globale.
49
49
50
-
The same thing for loops: `var`cannot be block- or loop-local:
50
+
La stessa cosa accade con i cicli: `var`non può essere locale ad un blocco/ciclo:
51
51
52
52
```js
53
53
for (var i =0; i <10; i++) {
54
54
// ...
55
55
}
56
56
57
57
*!*
58
-
alert(i); // 10, "i" is visible after loop, it's a global variable
58
+
alert(i); // 10, "i" è visibile anche dopo il ciclo, è una variabile globale
59
59
*/!*
60
60
```
61
61
62
-
If a code block is inside a function, then`var`becomes a function-level variable:
62
+
Se un blocco di codice si trova all'interno di una funzione, allora`var`diventa una variabile a livello di funzione:
63
63
64
64
```js
65
65
functionsayHi() {
66
66
if (true) {
67
67
var phrase ="Hello";
68
68
}
69
69
70
-
alert(phrase); //works
70
+
alert(phrase); //funziona
71
71
}
72
72
73
73
sayHi();
74
-
alert(phrase); //Error: phrase is not defined
74
+
alert(phrase); //Errore: phrase non è definito
75
75
```
76
76
77
-
As we can see, `var`pierces through`if`, `for`or other code blocks. That's because a long time ago in JavaScript blocks had no Lexical Environments. And`var`is a remnant of that.
77
+
Come possiamo vedere, `var`passa attraverso`if`, `for`o altri blocchi di codice. Questo accade perché molto tempo fa i blocchi JavaScript non possedevano un Lexical Environments. E`var`ne è un ricordo.
78
78
79
-
## "var" are processed at the function start
79
+
## "var" viene processata all'inizio della funzione
80
80
81
-
`var`declarations are processed when the function starts (or script starts for globals).
81
+
Le dichiarazioni con `var`vengono processata quando la funzione inizia (o lo script, nel caso delle variabili globali).
82
82
83
-
In other words, `var`variables are defined from the beginning of the function, no matter where the definition is (assuming that the definition is not in the nested function).
83
+
In altre parole, le variabili `var`sono definite dall'inizio della funzione, non ha importanza dove vengano definite (ovviamente non vale nel caso di funzioni annidate).
84
84
85
-
So this code:
85
+
Guardate questo esempio:
86
86
87
87
```js
88
88
functionsayHi() {
@@ -96,7 +96,7 @@ function sayHi() {
96
96
}
97
97
```
98
98
99
-
...Is technically the same as this (moved`var phrase` above):
99
+
...E' tecnicamente la stessa cosa di (spostando`var phrase`):
100
100
101
101
```js
102
102
functionsayHi() {
@@ -110,7 +110,7 @@ function sayHi() {
110
110
}
111
111
```
112
112
113
-
...Or even as this (remember, code blocks are ignored):
113
+
...O anche di questa (ricordate, i blocchi di codice vengono attraversati dallo scope della variabile):
114
114
115
115
```js
116
116
functionsayHi() {
@@ -126,13 +126,13 @@ function sayHi() {
126
126
}
127
127
```
128
128
129
-
People also call such behavior "hoisting" (raising), because all`var`are "hoisted" (raised) to the top of the function.
129
+
Questo comportamento viene chiamato "sollevamento", perché tutte`var`vengono "sollevate" fino all'inizio della funzione.
130
130
131
-
So in the example above, `if (false)`branch never executes, but that doesn't matter. The`var`inside it is processed in the beginning of the function, so at the moment of`(*)`the variable exists.
131
+
Quindi nell'esempio sopra, `if (false)`il ramo non eseguirà mai, ma non ha importanza. La`var`all'interno viene processata all'inizio della funzione, quindi quando ci troviamo in`(*)`la variabile esiste.
132
132
133
-
**Declarations are hoisted, but assignments are not.**
133
+
**Le dichiarazioni vengono sollevate, le assegnazioni no.**
134
134
135
-
That's better to demonstrate with an example, like this:
135
+
Lo dimostriamo con un esempio, come quello seguente:
136
136
137
137
```js run
138
138
functionsayHi() {
@@ -146,40 +146,40 @@ function sayHi() {
146
146
sayHi();
147
147
```
148
148
149
-
The line`var phrase = "Hello"`has two actions in it:
149
+
La riga`var phrase = "Hello"`può essere suddivisa in due:
150
150
151
-
1.Variable declaration`var`
152
-
2.Variable assignment`=`.
151
+
1.Dichiarazione della variabile`var`
152
+
2.Assegnazione della variabile con`=`.
153
153
154
-
The declaration is processed at the start of function execution ("hoisted"), but the assignment always works at the place where it appears. So the code works essentially like this:
154
+
La dichiarazione viene processata all'inizio della funzione ("sollevata"), l'assegnazione invece ha luogo sempre nel posto in cui appare. Quindi il codice funziona in questo modo:
155
155
156
156
```js run
157
157
functionsayHi() {
158
158
*!*
159
-
var phrase; //declaration works at the start...
159
+
var phrase; //la dichiarazione viene processata...
160
160
*/!*
161
161
162
162
alert(phrase); // undefined
163
163
164
164
*!*
165
-
phrase ="Hello"; // ...assignment - when the execution reaches it.
165
+
phrase ="Hello"; // ...assegnazione - quando viene raggiunta dal flusso d'esecuzione.
166
166
*/!*
167
167
}
168
168
169
169
sayHi();
170
170
```
171
171
172
-
Because all `var`declarations are processed at the function start, we can reference them at any place. But variables are undefined until the assignments.
172
+
Il fatto che la dichiarazione di `var`venga processata all'inizio della funzione, ci consente di farne riferimento in qualsiasi punto. Ma la variabile rimane `undefined` fino all'assegnazione.
173
173
174
-
In both examples above `alert`runs without an error, because the variable`phrase`exists. But its value is not yet assigned, so it shows`undefined`.
174
+
In entrambi gli esempi sopra `alert`esegue senza errori, poiché la variabile`phrase`esiste. Il suo valore però non gli è ancora stato assegnato, quindi viene mostrato`undefined`.
175
175
176
-
## Summary
176
+
## Riepilogo
177
177
178
-
There are two main differences of`var`:
178
+
Ci sono due principali differenze con`var`:
179
179
180
-
1.Variables have no block scope, they are visible minimum at the function level.
181
-
2.Variable declarations are processed at function start.
180
+
1.Le variabili non hanno uno scope locale al blocco, sono infatti visibili a livello di funzione.
181
+
2.La dichiarazione di variabili viene processata all'inizio della funzione.
182
182
183
-
There's one more minor difference related to the global object, we'll cover that in the next chapter.
183
+
C'è un ulteriore differenza di minore importanza legata all'oggetto globale, che andremo ad analizzare nel prossimo capitolo.
184
184
185
-
These differences are actually a bad thing most of the time. Block-level variables is such a great thing. That's why `let` was introduced in the standard long ago, and is now a major way (along with `const`) to declare a variable.
185
+
L'insieme di queste differenze fa si che `var` venga considerato uno svantaggio. Come prima cosa, non possiamo creare delle variabili locali al blocco. Il "sollevameto" genera solamente confusione. Quindi, negli script più recenti `var` viene utilizzato solamente in casi eccezionali.
0 commit comments