Skip to content

Global object #3415

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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
9 changes: 5 additions & 4 deletions 1-js/06-advanced-functions/05-global-object/article.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ The global object provides variables and functions that are available anywhere.

In a browser it is named `window`, for Node.js it is `global`, for other environments it may have another name.

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.
Recently, `globalThis` was added to the language as a standardized name for a global object intended to be supported across all environments. It's supported in all major browsers.

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.

Expand Down Expand Up @@ -48,7 +48,7 @@ window.currentUser = {
*/!*

// somewhere else in code
alert(currentUser.name); // John
alert(currentUser.name); // John

// or, if we have a local variable with the name "currentUser"
// get it from window explicitly (safe!)
Expand All @@ -61,14 +61,15 @@ That said, using global variables is generally discouraged. There should be as f

We use the global object to test for support of modern language features.

For instance, test if a built-in `Promise` object exists (it doesn't in really old browsers):
For instance, to test if a built-in `Promise` object exists (it doesn't in really old browsers):

```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.
If there's none (say, we're in an old browser), we can create "polyfills": add functions that are not supported by that environment, but exist in the modern standard.

```js run
if (!window.Promise) {
Expand Down