Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
bogdanbacosca wants to merge6 commits intojavascript-tutorial:master
base:master
Choose a base branch
Loading
frombogdanbacosca:global-object
Open
Changes fromall 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 deletions1-js/06-advanced-functions/05-global-object/article.md
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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 bythe 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 bythat environment, but exist in the modern standard.

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

[8]ページ先頭

©2009-2025 Movatter.jp