Skip to content

Global object #181

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 23, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 34 additions & 34 deletions 1-js/06-advanced-functions/05-global-object/article.md
Original file line number Diff line number Diff line change
@@ -1,89 +1,89 @@

# Global object
# Obiect global

The global object provides variables and functions that are available anywhere. By default, those that are built into the language or the environment.
Obiectul global oferă variabile și funcții care sunt disponibile oriunde. În mod implicit, cele care sunt încorporate în limbaj sau în mediu.

In a browser it is named `window`, for Node.js it is `global`, for other environments it may have another name.
Într-un browser se numește `window`, pentru Node.js este `global`, iar pentru alte medii poate avea un alt nume.

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

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

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

```js run
alert("Hello");
// is the same as
window.alert("Hello");
alert("Bună ziua");
// este la fel ca
window.alert("Bună ziua");
```

In a browser, global functions and variables declared with `var` (not `let/const`!) become the property of the global object:
Într-un browser, funcțiile și variabilele globale declarate cu `var` (nu cu `let/const`!) devin proprietatea obiectului global:

```js run untrusted refresh
var gVar = 5;

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

Function declarations have the same effect (statements with `function` keyword in the main code flow, not function expressions).
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).

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

If we used `let` instead, such thing wouldn't happen:
Dacă am folosi în schimb `let`, un astfel de lucru nu s-ar întâmpla:

```js run untrusted refresh
let gLet = 5;

alert(window.gLet); // undefined (doesn't become a property of the global object)
alert(window.gLet); // undefined (nu devine o proprietate a obiectului global)
```

If a value is so important that you'd like to make it available globally, write it directly as a property:
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:

```js run
*!*
// make current user information global, to let all scripts access it
// face informațiile despre utilizatorul curent globale, pentru a lăsa toate scripturile să le acceseze
window.currentUser = {
name: "John"
};
*/!*

// somewhere else in code
// în altă parte în cod
alert(currentUser.name); // John

// or, if we have a local variable with the name "currentUser"
// get it from window explicitly (safe!)
// sau, dacă avem o variabilă locală cu numele "currentUser"
// luați-o din window în mod explicit (sigur!)
alert(window.currentUser.name); // John
```

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

## Using for polyfills
## Utilizarea pentru polyfills

We use the global object to test for support of modern language features.
Utilizăm obiectul global pentru a testa suportul caracteristicilor moderne ale limbajului.

For instance, test if a built-in `Promise` object exists (it doesn't in really old browsers):
De exemplu, testăm dacă există un obiect `Promise` încorporat (nu există în browserele foarte vechi):
```js run
if (!window.Promise) {
alert("Your browser is really old!");
}
```

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

```js run
if (!window.Promise) {
window.Promise = ... // custom implementation of the modern language feature
window.Promise = ... // implementare personalizată a funcției de limbaj modern
}
```

## Summary
## Sumar

- The global object holds variables that should be available everywhere.
- Obiectul global deține variabile care ar trebui să fie disponibile peste tot.

That includes JavaScript built-ins, such as `Array` and environment-specific values, such as `window.innerHeight` -- the window height in the browser.
- The global object has a universal name `globalThis`.
Aceasta include integrări JavaScript, cum ar fi `Array` și valorile specifice mediului, cum ar fi `window.innerHeight` -- înălțimea ferestrei în browser.
- Obiectul global are un nume universal `globalThis`.

...But more often is referred by "old-school" environment-specific names, such as `window` (browser) and `global` (Node.js).
- We should store values in the global object only if they're truly global for our project. And keep their number at minimum.
- In-browser, unless we're using [modules](info:modules), global functions and variables declared with `var` become a property of the global object.
- To make our code future-proof and easier to understand, we should access properties of the global object directly, as `window.x`.
...Dar cel mai adesea este menționat prin nume "old-school" specifice mediului, cum ar fi `window` (browser) și `global` (Node.js).
- 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.
- Î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.
- 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`.