Skip to content

Commit 2758dc2

Browse files
Merge pull request #181
Global object
2 parents da846e9 + 9a7ffa1 commit 2758dc2

File tree

1 file changed

+34
-34
lines changed
  • 1-js/06-advanced-functions/05-global-object

1 file changed

+34
-34
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,89 @@
11

2-
# Global object
2+
# Obiect global
33

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

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

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

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

12-
All properties of the global object can be accessed directly:
12+
Toate proprietățile obiectului global pot fi accesate direct:
1313

1414
```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");
1818
```
1919

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

2222
```js run untrusted refresh
2323
var gVar = 5;
2424

25-
alert(window.gVar); // 5 (became a property of the global object)
25+
alert(window.gVar); // 5 (a devenit o proprietate a obiectului global)
2626
```
2727

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

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

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

3434
```js run untrusted refresh
3535
let gLet = 5;
3636

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)
3838
```
3939

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

4242
```js run
4343
*!*
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
4545
window.currentUser = {
4646
name: "John"
4747
};
4848
*/!*
4949

50-
// somewhere else in code
50+
// în altă parte în cod
5151
alert(currentUser.name); // John
5252

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!)
5555
alert(window.currentUser.name); // John
5656
```
5757

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

60-
## Using for polyfills
60+
## Utilizarea pentru polyfills
6161

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

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):
6565
```js run
6666
if (!window.Promise) {
6767
alert("Your browser is really old!");
6868
}
6969
```
7070

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

7373
```js run
7474
if (!window.Promise) {
75-
window.Promise = ... // custom implementation of the modern language feature
75+
window.Promise = ... // implementare personalizată a funcției de limbaj modern
7676
}
7777
```
7878

79-
## Summary
79+
## Sumar
8080

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

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

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

Comments
 (0)