|
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 |
| -## Using for polyfills |
| 60 | +## Utilizarea pentru polyfills |
61 | 61 |
|
62 |
| -We use the global object to test for support of modern language features. |
| 62 | +Utilizăm obiectul global pentru a testa suportul caracteristicilor moderne ale limbajului. |
63 | 63 |
|
64 |
| -For instance, test if a built-in `Promise` object exists (it doesn't in really old browsers): |
| 64 | +De exemplu, testăm dacă există un obiect `Promise` încorporat (nu există în browserele foarte vechi): |
65 | 65 | ```js run
|
66 | 66 | if (!window.Promise) {
|
67 | 67 | alert("Your browser is really old!");
|
68 | 68 | }
|
69 | 69 | ```
|
70 | 70 |
|
71 |
| -If there's none (say, we're in an old browser), we can create "polyfills": add functions that are not supported by the environment, but exist in the modern standard. |
| 71 | +Dacă nu există (să spunem că ne aflăm într-un browser vechi), putem crea "polyfills": adăugăm funcții care nu sunt acceptate de mediul respectiv, dar care există în standardul modern. |
72 | 72 |
|
73 | 73 | ```js run
|
74 | 74 | if (!window.Promise) {
|
75 |
| - window.Promise = ... // custom implementation of the modern language feature |
| 75 | + window.Promise = ... // implementare personalizată a funcției de limbaj modern |
76 | 76 | }
|
77 | 77 | ```
|
78 | 78 |
|
79 |
| -## Summary |
| 79 | +## Sumar |
80 | 80 |
|
81 |
| -- The global object holds variables that should be available everywhere. |
| 81 | +- Obiectul global deține variabile care ar trebui să fie disponibile peste tot. |
82 | 82 |
|
83 |
| - That includes JavaScript built-ins, such as `Array` and environment-specific values, such as `window.innerHeight` -- the window height in the browser. |
84 |
| -- The global object has a universal name `globalThis`. |
| 83 | + Aceasta include integrări JavaScript, cum ar fi `Array` și valorile specifice mediului, cum ar fi `window.innerHeight` -- înălțimea ferestrei în browser. |
| 84 | +- Obiectul global are un nume universal `globalThis`. |
85 | 85 |
|
86 |
| - ...But more often is referred by "old-school" environment-specific names, such as `window` (browser) and `global` (Node.js). |
87 |
| -- We should store values in the global object only if they're truly global for our project. And keep their number at minimum. |
88 |
| -- In-browser, unless we're using [modules](info:modules), global functions and variables declared with `var` become a property of the global object. |
89 |
| -- To make our code future-proof and easier to understand, we should access properties of the global object directly, as `window.x`. |
| 86 | + ...Dar cel mai adesea este menționat prin nume "old-school" specifice mediului, cum ar fi `window` (browser) și `global` (Node.js). |
| 87 | +- Ar trebui să stocăm valori în obiectul global numai dacă acestea sunt cu adevărat globale pentru proiectul nostru. Și să păstrăm numărul lor la minim. |
| 88 | +- În browser, cu excepția cazului în care folosim [module](info:modules), funcțiile și variabilele globale declarate cu `var` devin o proprietate a obiectului global. |
| 89 | +- Pentru a face codul nostru rezistent pe viitor și mai ușor de înțeles, ar trebui să accesăm direct proprietățile obiectului global, ca `window.x`. |
0 commit comments