Skip to content

Commit cb1600b

Browse files
committed
Add 1-js/06-advanced-functions/04-var/article.md
1 parent 17fdf66 commit cb1600b

1 file changed

Lines changed: 45 additions & 45 deletions

File tree

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

2-
# The old "var"
2+
# Il vecchio "var"
33

4-
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:
55

66
1. `let`
77
2. `const`
88
3. `var`
99

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

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

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

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:
1717

1818
```js run
1919
function sayHi() {
20-
var phrase = "Hello"; // local variable, "var" instead of "let"
20+
var phrase = "Hello"; // variabile locale, "var" piuttosto di "let"
2121

2222
alert(phrase); // Hello
2323
}
2424

2525
sayHi();
2626

27-
alert(phrase); // Error, phrase is not defined
27+
alert(phrase); // Errore, phrase non è definito
2828
```
2929

30-
...But here are the differences.
30+
...Abbiamo trovato una differenza.
3131

32-
## "var" has no block scope
32+
## "var" non ha uno scope di blocco
3333

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

36-
For instance:
36+
Ad esempio:
3737

3838
```js
3939
if (true) {
40-
var test = true; // use "var" instead of "let"
40+
var test = true; // utilizziamo "var" piuttosto di "let"
4141
}
4242

4343
*!*
44-
alert(test); // true, the variable lives after if
44+
alert(test); // vero, la variabile vive dopo if
4545
*/!*
4646
```
4747

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

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:
5151

5252
```js
5353
for (var i = 0; i < 10; i++) {
5454
// ...
5555
}
5656

5757
*!*
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
5959
*/!*
6060
```
6161

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:
6363

6464
```js
6565
function sayHi() {
6666
if (true) {
6767
var phrase = "Hello";
6868
}
6969

70-
alert(phrase); // works
70+
alert(phrase); // funziona
7171
}
7272

7373
sayHi();
74-
alert(phrase); // Error: phrase is not defined
74+
alert(phrase); // Errore: phrase non è definito
7575
```
7676

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

79-
## "var" are processed at the function start
79+
## "var" viene processata all'inizio della funzione
8080

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).
8282

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).
8484

85-
So this code:
85+
Guardate questo esempio:
8686

8787
```js
8888
function sayHi() {
@@ -96,7 +96,7 @@ function sayHi() {
9696
}
9797
```
9898

99-
...Is technically the same as this (moved `var phrase` above):
99+
...E' tecnicamente la stessa cosa di (spostando `var phrase`):
100100

101101
```js
102102
function sayHi() {
@@ -110,7 +110,7 @@ function sayHi() {
110110
}
111111
```
112112

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

115115
```js
116116
function sayHi() {
@@ -126,13 +126,13 @@ function sayHi() {
126126
}
127127
```
128128

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

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

133-
**Declarations are hoisted, but assignments are not.**
133+
**Le dichiarazioni vengono sollevate, le assegnazioni no.**
134134

135-
That's better to demonstrate with an example, like this:
135+
Lo dimostriamo con un esempio, come quello seguente:
136136

137137
```js run
138138
function sayHi() {
@@ -146,40 +146,40 @@ function sayHi() {
146146
sayHi();
147147
```
148148

149-
The line `var phrase = "Hello"` has two actions in it:
149+
La riga `var phrase = "Hello"` può essere suddivisa in due:
150150

151-
1. Variable declaration `var`
152-
2. Variable assignment `=`.
151+
1. Dichiarazione della variabile `var`
152+
2. Assegnazione della variabile con `=`.
153153

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:
155155

156156
```js run
157157
function sayHi() {
158158
*!*
159-
var phrase; // declaration works at the start...
159+
var phrase; // la dichiarazione viene processata...
160160
*/!*
161161

162162
alert(phrase); // undefined
163163

164164
*!*
165-
phrase = "Hello"; // ...assignment - when the execution reaches it.
165+
phrase = "Hello"; // ...assegnazione - quando viene raggiunta dal flusso d'esecuzione.
166166
*/!*
167167
}
168168

169169
sayHi();
170170
```
171171

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

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`.
175175

176-
## Summary
176+
## Riepilogo
177177

178-
There are two main differences of `var`:
178+
Ci sono due principali differenze con `var`:
179179

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

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

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

Comments
 (0)