Skip to content

Commit 4f2a80d

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

1 file changed

Lines changed: 42 additions & 42 deletions

File tree

  • 1-js/06-advanced-functions/05-global-object
Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,57 @@
11

2-
# Global object
2+
# Oggetto globale
33

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

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

8-
For instance, we can call `alert` as a method of `window`:
8+
Ad esempio, potremmo invocare `alert` come metodo di `window`:
99

1010
```js run
1111
alert("Hello");
1212

13-
// the same as
13+
// la stessa cosa
1414
window.alert("Hello");
1515
```
1616

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à.
1818

19-
## Browser: the "window" object
19+
## Browser: l'oggetto "window"
2020

21-
For historical reasons, in-browser `window` object is a bit messed up.
21+
Per ragioni storiche, l'oggetto `window` è leggermente incasinato.
2222

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

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

2727
```js run
28-
alert(window.innerHeight); // shows the browser window height
28+
alert(window.innerHeight); // mostra l'altezza della window
2929

30-
window.open('http://google.com'); // opens a new browser window
30+
window.open('http://google.com'); // apre una nuova browser window
3131
```
3232

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

35-
For instance:
35+
Ad esempio:
3636
```js untrusted run no-strict refresh
3737
var x = 5;
3838
39-
alert(window.x); // 5 (var x becomes a property of window)
39+
alert(window.x); // 5 (var x diventa una proprietà di window)
4040
4141
window.x = 0;
4242
43-
alert(x); // 0, variable modified
43+
alert(x); // 0, variabile modificata
4444
```
4545

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`:
4747

4848
```js untrusted run no-strict refresh
4949
let x = 5;
5050
51-
alert(window.x); // undefined ("let" doesn't create a window property)
51+
alert(window.x); // undefined ("let" non crea proprietà sull'oggetto window)
5252
```
5353

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:
5555
5656
```html run
5757
<script>
@@ -65,25 +65,25 @@ For historical reasons, in-browser `window` object is a bit messed up.
6565
</script>
6666
```
6767
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`.
6969
7070
```js untrusted run no-strict refresh
7171
alert(this); // window
7272
```
7373
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.
7575

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?
7777
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.
7979
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.
8181
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".
8383
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`.
8585
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`:
8787
8888
```html run
8989
<script type="module">
@@ -93,7 +93,7 @@ If we set `type="module"` attribute on a `<script>` tag, then such script is con
9393
</script>
9494
```
9595
96-
- Two modules that do not see variables of each other:
96+
- Due moduli diversi non condividono le variabili:
9797
9898
```html run
9999
<script type="module">
@@ -102,54 +102,54 @@ If we set `type="module"` attribute on a `<script>` tag, then such script is con
102102
103103
<script type="module">
104104
alert(window.x); // undefined
105-
alert(x); // Error: undeclared variable
105+
alert(x); // Error: variabile non dichiarata
106106
</script>
107107
```
108108
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`?):
110110
111111
```html run
112112
<script type="module">
113113
alert(this); // undefined
114114
</script>
115115
```
116116
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`.**
118118

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

121-
## Valid uses of the global object
121+
## Utilizzi sensati dell'oggetto globale
122122
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).
124124
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:
126126
127127
```js run
128-
// explicitly assign it to `window`
128+
// assegnazione esplicita a `window`
129129
window.currentUser = {
130130
name: "John",
131131
age: 30
132132
};
133133
134-
// then, elsewhere, in another script
134+
// poi, in un qualsiasi punto di un altro script
135135
alert(window.currentUser.name); // John
136136
```
137137
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.
139139

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):
141141
```js run
142142
if (!window.Promise) {
143143
alert("Your browser is really old!");
144144
}
145145
```
146146
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.
148148

149149
```js run
150150
if (!window.Promise) {
151-
window.Promise = ... // custom implementation of the modern language feature
151+
window.Promise = ... // implementazione di una caratteristica moderna
152152
}
153153
```
154154

155-
...And of course, if we're in a browser, using `window` to access browser window features (not as a global object) is completely fine.
155+
...E ovviamente, se ci troviamo in ambiente browser, possiamo utilizzare `window` per accedere alle caratteristiche della finestra (senza utilizzarlo come oggetto globale).

0 commit comments

Comments
 (0)