|
1 | 1 |
|
2 |
| -# Global object |
| 2 | +# Obiect global |
3 | 3 |
|
4 |
| -The global object provides variables and functions that are available anywhere. By default, those that are built into the language or the environment. |
| 4 | +Obiectul global oferă variabile și funcții care sunt disponibile oriunde. În mod implicit, cele care sunt încorporate în limbaj sau în mediu. |
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 | +Într-un browser se numește `window`, pentru Node.js este `global`, iar pentru alte medii poate avea un alt nume. |
7 | 7 |
|
8 |
| -Recently, `globalThis` was added to the language, as a standardized name for a global object, that should be supported across all environments. It's supported in all major browsers. |
| 8 | +Recent, `globalThis` a fost adăugat în limbaj, ca un nume standardizat pentru un obiect global, care ar trebui să fie acceptat în toate mediile. Este suportat în toate browserele majore. |
9 | 9 |
|
10 |
| -We'll use `window` here, assuming that our environment is a browser. If your script may run in other environments, it's better to use `globalThis` instead. |
| 10 | +Vom folosi `window` aici, presupunând că mediul nostru este un browser. Dacă scriptul dvs. ar poate rula în alte medii, este mai bine să folosiți `globalThis` în schimb. |
11 | 11 |
|
12 |
| -All properties of the global object can be accessed directly: |
| 12 | +Toate proprietățile obiectului global pot fi accesate direct: |
13 | 13 |
|
14 | 14 | ```js run
|
15 |
| -alert("Hello"); |
16 |
| -// is the same as |
17 |
| -window.alert("Hello"); |
| 15 | +alert("Bună ziua"); |
| 16 | +// este la fel ca |
| 17 | +window.alert("Bună ziua"); |
18 | 18 | ```
|
19 | 19 |
|
20 |
| -In a browser, global functions and variables declared with `var` (not `let/const`!) become the property of the global object: |
| 20 | +Într-un browser, funcțiile și variabilele globale declarate cu `var` (nu cu `let/const`!) devin proprietatea obiectului global: |
21 | 21 |
|
22 | 22 | ```js run untrusted refresh
|
23 | 23 | var gVar = 5;
|
24 | 24 |
|
25 |
| -alert(window.gVar); // 5 (became a property of the global object) |
| 25 | +alert(window.gVar); // 5 (a devenit o proprietate a obiectului global) |
26 | 26 | ```
|
27 | 27 |
|
28 |
| -Function declarations have the same effect (statements with `function` keyword in the main code flow, not function expressions). |
| 28 | +Declarațiile de funcții au același efect (declarații folosind cuvântul cheie `function` în fluxul principal de cod, nu expresii de funcții). |
29 | 29 |
|
30 |
| -Please don't rely on that! This behavior exists for compatibility reasons. Modern scripts use [JavaScript modules](info:modules) where such a thing doesn't happen. |
| 30 | +Vă rugăm să nu vă bazați pe asta! Acest comportament există din motive de compatibilitate. Scripturile moderne folosesc [module JavaScript](info:modules) unde nu se întâmplă așa ceva. |
31 | 31 |
|
32 |
| -If we used `let` instead, such thing wouldn't happen: |
| 32 | +Dacă am folosi în schimb `let`, un astfel de lucru nu s-ar întâmpla: |
33 | 33 |
|
34 | 34 | ```js run untrusted refresh
|
35 | 35 | let gLet = 5;
|
36 | 36 |
|
37 |
| -alert(window.gLet); // undefined (doesn't become a property of the global object) |
| 37 | +alert(window.gLet); // undefined (nu devine o proprietate a obiectului global) |
38 | 38 | ```
|
39 | 39 |
|
40 |
| -If a value is so important that you'd like to make it available globally, write it directly as a property: |
| 40 | +Dacă o valoare este atât de importantă încât doriți să o faceți disponibilă la nivel global, scrieți-o direct ca o proprietate: |
41 | 41 |
|
42 | 42 | ```js run
|
43 | 43 | *!*
|
44 |
| -// make current user information global, to let all scripts access it |
| 44 | +// face informațiile despre utilizatorul curent globale, pentru a lăsa toate scripturile să le acceseze |
45 | 45 | window.currentUser = {
|
46 | 46 | name: "John"
|
47 | 47 | };
|
48 | 48 | */!*
|
49 | 49 |
|
50 |
| -// somewhere else in code |
| 50 | +// în altă parte în cod |
51 | 51 | alert(currentUser.name); // John
|
52 | 52 |
|
53 |
| -// or, if we have a local variable with the name "currentUser" |
54 |
| -// get it from window explicitly (safe!) |
| 53 | +// sau, dacă avem o variabilă locală cu numele "currentUser" |
| 54 | +// luați-o din window în mod explicit (sigur!) |
55 | 55 | alert(window.currentUser.name); // John
|
56 | 56 | ```
|
57 | 57 |
|
58 |
| -That said, using global variables is generally discouraged. There should be as few global variables as possible. The code design where a function gets "input" variables and produces certain "outcome" is clearer, less prone to errors and easier to test than if it uses outer or global variables. |
| 58 | +Acestea fiind spuse, utilizarea variabilelor globale este în general descurajată. Ar trebui să fie cât mai puține variabile globale pe cât posibil. Proiectarea codului în care o funcție primește variabile de "intrare" și produce un anumit "rezultat" este mai clară, mai puțin predispusă la erori și mai ușor de testat decât în cazul în care folosește variabile exterioare sau globale. |
59 | 59 |
|
60 | 60 | ## Using for polyfills
|
61 | 61 |
|
|
0 commit comments