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
The global object provides variables and functions that are available anywhere. Mostly, the ones that are built into the language or the host environment.
4
+
L'oggetto globale fornisce variabili e funzioni che sono accessibili in qualsiasi punto. Principalmente quelle integrate dal linguaggio o fornite dall'ambiente.
5
5
6
-
In a browser it is named "window", for Node.js it is "global", for other environments it may have another name.
6
+
In un browser l'ambiente si chiama "window", per Node.js viene detto "global", negli altri ambienti si usano diversi termini.
7
7
8
-
For instance, we can call `alert`as a method of`window`:
8
+
Ad esempio, potremmo invocare `alert`come metodo di`window`:
9
9
10
10
```js run
11
11
alert("Hello");
12
12
13
-
//the same as
13
+
//la stessa cosa
14
14
window.alert("Hello");
15
15
```
16
16
17
-
We can reference other built-in functions like `Array`as`window.Array`and create our own properties on it.
17
+
LO stesso vale per tutte le altre funzioni integrate, ad esempio possiamo invocare `Array`come`window.Array`e creare le nostre personali proprietà.
18
18
19
-
## Browser: the "window" object
19
+
## Browser: l'oggetto "window"
20
20
21
-
For historical reasons, in-browser`window`object is a bit messed up.
21
+
Per ragioni storiche, l'oggetto`window`è leggermente incasinato.
22
22
23
-
1.It provides the "browser window" functionality, besides playing the role of a global object.
23
+
1.Fornisce la funzionalità di "finestra del browser", oltre a svolgere il ruolo di oggetto globale.
24
24
25
-
We can use `window`to access properties and methods, specific to the browser window:
25
+
Possiamo utilizzare `window`per accedere a proprietà e metodi, specifici di una finestra del browser:
26
26
27
27
```js run
28
-
alert(window.innerHeight); //shows the browser window height
28
+
alert(window.innerHeight); //mostra l'altezza della window
29
29
30
-
window.open('http://google.com'); //opens a new browser window
30
+
window.open('http://google.com'); //apre una nuova browser window
31
31
```
32
32
33
-
2.Top-level `var`variables and function declarations automatically become properties of `window`.
33
+
2.Le variabili `var`e le dichiarazioni di funzioni diventano automaticamente delle proprietà di`window`.
34
34
35
-
For instance:
35
+
Ad esempio:
36
36
```js untrusted run no-strict refresh
37
37
var x = 5;
38
38
39
-
alert(window.x); // 5 (var x becomes a property of window)
39
+
alert(window.x); // 5 (var x diventa una proprietà di window)
40
40
41
41
window.x = 0;
42
42
43
-
alert(x); // 0, variable modified
43
+
alert(x); // 0, variabile modificata
44
44
```
45
45
46
-
Please note, that doesn't happen with more modern `let/const` declarations:
46
+
Da notare che tutto ciò non vale per le dichiarazioni `let/const`:
47
47
48
48
```js untrusted run no-strict refresh
49
49
let x = 5;
50
50
51
-
alert(window.x); // undefined ("let" doesn't create a window property)
51
+
alert(window.x); // undefined ("let" non crea proprietà sull'oggetto window)
52
52
```
53
53
54
-
3. Also, all scripts share the same global scope, so variables declared in one `<script>` become visible in another ones:
54
+
3.Inoltre, tutti gli script condividono lo stesso scope globale, quindi le variabili dichiarate all'interno di uno `<script>` diventano visibili anche negli altri:
55
55
56
56
```html run
57
57
<script>
@@ -65,25 +65,25 @@ For historical reasons, in-browser `window` object is a bit messed up.
65
65
</script>
66
66
```
67
67
68
-
4. And, a minor thing, but still: the value of `this` in the global scope is `window`.
68
+
4. Un ultima cosa, il valore di `this` nello scope globale è `window`.
69
69
70
70
```js untrusted run no-strict refresh
71
71
alert(this); // window
72
72
```
73
73
74
-
Why was it made like this? At the time of the language creation, the idea to merge multiple aspects into a single `window` object was to "make things simple". But since then many things changed. Tiny scripts became big applications that require proper architecture.
74
+
Perché è stato fatto cosi? Nel momento in cui è stato creato il linguaggio, l'idea era quella di fondere diversi aspetti in un unico oggetto `window`per "rendere le cose più semplici". Ma da quel momento sono cambiate molte cose. Da piccoli script si è passati a grandi applicazioni le quali richiedono una propria architettura.
75
75
76
-
Is it good that different scripts (possibly from different sources) see variables of each other?
76
+
E' una cosa buona che diversi script (eventualmente anche provenienti da altri sviluppatori) si vedano le variabili a vicenda?
77
77
78
-
No, it's not, because it may lead to naming conflicts: the same variable name can be used in two scripts for different purposes, so they will conflict with each other.
78
+
Ovviamente no, questa caratteristica potrebbe portare ad errori dovuti a conflitti tra nomi: due variabili (di script diversi) con uno stesso nome potrebbero aver scopi differenti nei diversi script in cui vengono utilizzate.
79
79
80
-
As of now, the multi-purpose `window` is considered a design mistake in the language.
80
+
Ad oggi, questa caratteristica di `window` viene considerata un errore nel design del linguaggio.
81
81
82
-
Luckily, there's a "road out of hell", called "JavaScript modules".
82
+
Fortunatamente esiste una soluzione per "aggirare" questo problema, ed è chiamata "JavaScript module".
83
83
84
-
If we set `type="module"` attribute on a `<script>` tag, then such script is considered a separate "module" with its own top-level scope (lexical environment), not interfering with `window`.
84
+
Se impostiamo `type="module"` come attributo su un tag `<script>`, allora questo script verrà considerato un "modulo" separato con il suo suo personale scope globale (lexical environment), e non interferirà con `window`.
85
85
86
-
- In a module, `var x` does not become a property of `window`:
86
+
- In un modulo, `var x` non diventerà una proprietà di `window`:
87
87
88
88
```html run
89
89
<script type="module">
@@ -93,7 +93,7 @@ If we set `type="module"` attribute on a `<script>` tag, then such script is con
93
93
</script>
94
94
```
95
95
96
-
- Two modules that do not see variables of each other:
96
+
- Due moduli diversi non condividono le variabili:
97
97
98
98
```html run
99
99
<script type="module">
@@ -102,54 +102,54 @@ If we set `type="module"` attribute on a `<script>` tag, then such script is con
102
102
103
103
<script type="module">
104
104
alert(window.x); // undefined
105
-
alert(x); // Error: undeclared variable
105
+
alert(x); // Error: variabile non dichiarata
106
106
</script>
107
107
```
108
108
109
-
- And, the last minor thing, the top-level value of `this` in a module is `undefined` (why should it be `window` anyway?):
109
+
- E come ultima cosa, il valore di `this` (al livello globale) in un modulo sarà `undefined` (non avrebbe alcun senso se contenesse `window`?):
110
110
111
111
```html run
112
112
<script type="module">
113
113
alert(this); // undefined
114
114
</script>
115
115
```
116
116
117
-
**Using `<script type="module">` fixes the design flaw of the language by separating top-level scope from `window`.**
117
+
**L'utilizzo di `<script type="module">`risolve i difetti di design del linguaggio, separando il livello massimo (top-level) da`window`.**
118
118
119
-
We'll cover more features of modules later, in the chapter [](info:modules).
119
+
Più avanti tratteremo più in dettaglio questo argomento, nel capitolo [](info:modules).
120
120
121
-
## Valid uses of the global object
121
+
## Utilizzi sensati dell'oggetto globale
122
122
123
-
1. Using global variables is generally discouraged. There should be as few global variables as possible, but if we need to make something globally visible, we may want to put it into `window` (or `global` in Node.js).
123
+
1. L'utilizzo delle variabili globali, solitamente è sconsigliato. Dovrebbero esserci il minor numero di variabili globali possibili, ma se proprio ne avessimo bisogno possiamo comunque sfruttare l'oggetto globale `window` (o `global` in Node.js).
124
124
125
-
Here we put the information about the current user into a global object, to be accessible from all other scripts:
125
+
Qui inseriamo delle informazioni riguardo l'utente nell'oggetto globale, in modo tale da renderle accessibili anche agli altri script:
126
126
127
127
```js run
128
-
//explicitly assign it to `window`
128
+
// assegnazione esplicita a `window`
129
129
window.currentUser = {
130
130
name: "John",
131
131
age: 30
132
132
};
133
133
134
-
//then, elsewhere, in another script
134
+
// poi, in un qualsiasi punto di un altro script
135
135
alert(window.currentUser.name); // John
136
136
```
137
137
138
-
2.We can test the global object for support of modern language features.
138
+
2. Possiamo testare l'oggetto globale per verificare la presenza di caratteristiche moderne.
139
139
140
-
For instance, test if a build-in`Promise`object exists (it doesn't in really old browsers):
140
+
Ad esempio, testiamo se esiste l'oggetto `Promise` (nei vecchi browser non era presente):
141
141
```js run
142
142
if (!window.Promise) {
143
143
alert("Your browser is really old!");
144
144
}
145
145
```
146
146
147
-
3. We can create "polyfills": add functions that are not supported by the environment (say, an old browser), but exist in the modern standard.
147
+
3. Possiamo creare "polyfills": aggiungere funzioni che non sono supportate dall'ambiente, ma che sono supportate dagli standard moderni.
148
148
149
149
```js run
150
150
if (!window.Promise) {
151
-
window.Promise = ... // custom implementation of the modern language feature
151
+
window.Promise = ... // implementazione di una caratteristica moderna
152
152
}
153
153
```
154
154
155
-
...And of course, if we're ina browser, using `window`to access browser windowfeatures (not as a global object) is completely fine.
155
+
...E ovviamente, se ci troviamo inambiente browser, possiamo utilizzare `window`per accedere alle caratteristiche della finestra (senza utilizzarlo come oggetto globale).
0 commit comments