全局
globalThis
特性包含全局
this
值,类似于全局对象。
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
特性属性在
globalThis
|
|
|---|---|
| 可写 | yes |
| 可枚举 | no |
| 可配置 | yes |
Historically, accessing the global object has required different syntax in different JavaScript environments. On the web you can use
window
,
self
,或
frames
- but in
Web 工作者
only
self
will work. In Node.js none of these work, and you must instead use
global
.
this
keyword could be used inside functions running in non–strict mode, but
this
will be
undefined
in Modules and inside functions running in strict mode. You can also use
Function('return this')()
, but environments that disable
eval()
, like
CSP
in browsers, prevent use of
Function
in this way.
globalThis
property provides a standard way of accessing the global
this
value (and hence the global object itself) across environments. Unlike similar properties such as
window
and
self
, it's guaranteed to work in window and non-window contexts. In this way, you can access the global object in a consistent manner without having to know which environment the code is being run in.To help you remember the name, just remember that in global scope the
this
value is
globalThis
.
In many engines
globalThis
will be a reference to the actual global object, but in web browsers, due to iframe and cross-window security considerations, it references a
Proxy
around the actual global object (which you can't directly access). This distinction is rarely relevant in common usage, but important to be aware of.
Several other popular name choices such as
self
and
global
were removed from consideration because of their potential to break compatibility with existing code. See the
language proposal's "naming" document
了解更多细节。
Prior to
globalThis
, the only reliable cross-platform way to get the global object for an environment was
Function('return this')()
. However, this causes
CSP
violations in some settings, so
es6-shim
uses a check like this, for example:
var getGlobal = function () {
if (typeof self !== 'undefined') { return self; }
if (typeof window !== 'undefined') { return window; }
if (typeof global !== 'undefined') { return global; }
throw new Error('unable to locate global object');
};
var globals = getGlobal();
if (typeof globals.setTimeout !== 'function') {
// no setTimeout in this environment!
}
With
globalThis
available, the additional search for the global across environments is not necessary anymore:
if (typeof globalThis.setTimeout !== 'function') {
// no setTimeout in this environment!
}
| 规范 |
|---|
|
ECMAScript (ECMA-262)
在该规范中的 globalThis 定义。 |
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
globalThis
|
Chrome 71 | Edge 79 | Firefox 65 | IE No | Opera 58 | Safari 12.1 | WebView Android 71 | Chrome Android 71 | Firefox Android 65 | Opera Android 50 | Safari iOS 12.2 | Samsung Internet Android 10.0 | nodejs 12.0.0 |
完整支持
不支持
The following table provides a daily implementation status for this feature, because this feature has not yet reached cross-browser stability. The data is generated by running the relevant feature tests in Test262 , the standard test suite of JavaScript, in the nightly build, or latest release of each browser's JavaScript engine.