-
Notifications
You must be signed in to change notification settings - Fork 29.6k
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
doc: document globalThis TC39 stage 3 proposal #27399
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -81,6 +81,18 @@ This variable may appear to be global but is not. See [`exports`]. | |
## global | ||
<!-- YAML | ||
added: v0.1.27 | ||
deprecated: REPLACEME | ||
--> | ||
|
||
<!-- type=global --> | ||
|
||
> Stability: 0 - Deprecated: Use [`globalThis)`][] instead. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should also have a deprecation code filed in |
||
|
||
* {Object} The global namespace object. | ||
|
||
## globalThis | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure we should document this as it's a JavaScript global. Everything else in globals.md is a Node.js specific global that is not available in "plain" JavaScript. That is - nothing else here is defined in the ECMAScript spec (we don't list Array, String etc here for example). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. well it's currently not in ecma262, it's still stage 3 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would suggest holding off for the time being |
||
<!-- YAML | ||
added: v11.0.0 | ||
--> | ||
|
||
<!-- type=global --> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
// Flags: --experimental-modules | ||
/* eslint-disable node-core/required-modules */ | ||
import '../common/index.mjs'; | ||
import('./test-esm-cyclic-dynamic-import.mjs'); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
// Flags: --experimental-modules | ||
/* eslint-disable node-core/required-modules */ | ||
import '../common/index.mjs'; | ||
import '../fixtures/es-module-loaders/syntax-error-import.mjs'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
// Flags: --experimental-modules | ||
/* eslint-disable node-core/required-modules */ | ||
import '../common/index.mjs'; | ||
import '../fixtures/es-module-loaders/syntax-error.mjs'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
'use strict'; | ||
|
||
require('../common'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am also not sure why we need a test for this as we don't have tests for other JavaScript features. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One reason is that it'll block this PR from accidentally getting back-ported to release branches that don't have |
||
const assert = require('assert'); | ||
|
||
assert.strictEqual(typeof globalThis, 'object'); | ||
assert.deepStrictEqual( | ||
Object.getOwnPropertyDescriptor(globalThis, 'globalThis'), | ||
{ configurable: true, enumerable: false, writable: true, value: globalThis }); | ||
assert.strictEqual(globalThis, global); | ||
assert.strictEqual(String(globalThis), '[object global]'); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// Flags: --experimental-modules | ||
import '../common/index.mjs'; | ||
import assert from 'assert'; | ||
|
||
assert.strictEqual(typeof globalThis, 'object'); | ||
assert.deepStrictEqual( | ||
Object.getOwnPropertyDescriptor(globalThis, 'globalThis'), | ||
{ configurable: true, enumerable: false, writable: true, value: globalThis }); | ||
assert.strictEqual(globalThis, global); | ||
assert.strictEqual(String(globalThis), '[object global]'); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Has the project decided to deprecate
global
now thatglobalThis
is available?I think there are still cases
global
is useful - in particular I've seen it used when probing for an environment that is Node.js and not "universal JavaScript".There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I personally have never seen code that checks for the presence of
global
, it's alwaysif (typeof process === 'object')
, but doc-deprecating it won't break that.The motivation for deprecation is that there should be One Way To Do It.
globalThis
seems strictly superior since it works in node and modern browsers.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Definitely don’t deprecate global; it’s so heavily used that it isn’t web compatible to name globalThis global :-)
In other words, there are many millions of examples in GitHub code search alone of code that does this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree it’s not possible to runtime deprecate global at this point but this document only doc-deprecate it? We may come back revisiting it or it could just stay doc deprecated forever but this sends a message to developers that they should use globalThis instead in new code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don’t think the message is needed. If they want to write universal code, they either can already rely on “every single bundler” handling global transparently for them, or they can use globalThis that works in both.
I just don’t think there’s any value or need to push people in a direction here.