globalThis S4
- Stage: Stage 4
- Status: Finished
- ECMAScript edition: ES2020
- Synchronized: Aug 28, 2026
- 中文译文 · Source repository
This proposal introduces globalThis, a standardized way to access the global object across different environments (browsers, Node.js, shells, etc.), solving the portability issues of existing methods. It defines globalThis as a writable and configurable property that maintains identity across navigation in HTML, distinguishing it from the underlying global object.
The README below comes from the upstream repository and may contain outdated stage or status metadata. Use the proposal details above as the current source of truth.
globalThis
ECMAScript Proposal, specs, and reference implementation for globalThis
Spec drafted by @ljharb.
This proposal is currently stage 4 of the process.
Rationale
It is difficult to write portable ECMAScript code which accesses the global object. On the web, it is accessible as window or self or this or frames; on node.js, it is global or this; among those, only this is available in a shell like V8's d8 or JavaScriptCore's jsc. In a standalone function call in sloppy mode, this works too, but it's undefined in modules or in strict mode within a function. In such contexts, the global object can still be accessed by Function('return this')(), but that form is inaccessible with some CSP settings, such as within Chrome Apps. Below is some code from the wild to get the global object, passed in as the single argument to an IIFE, which works for most cases but won't actually work in d8 when in a module or in strict mode inside a function (which could be fixed using the Function trick):
In addition, the es6-shim had to switch from Function('return this')() due to CSP concerns, such that the current check to handle browsers, node, web workers, and frames is:
HTML and the WindowProxy
In HTML, the global object is separated into the Window and the WindowProxy. New attributes are set on the Window, but top-level this has the identity of the WindowProxy. The WindowProxy forwards all object operations to the underlying Window, but as the page changes, globalThis maintains the same identity while the underlying Window is swapped out.
The distinction is observable in the following scenario, with files parent.html, frame-a.html, and frame-b.html. frame-a.html has the following source code:
frame-b.html has the following source code:
parent.html’s source code is:
This demo shows that the global variable foo was being stored on the actual global object, which has changed during navigation, but globalThis has not changed during navigation. Therefore, globalThis is not the global object.
Thus, globalThis is observably different from “the global object”, which is not directly accessible from JavaScript. In web browsers, it’s even possible that (even in the global scope), foo !== globalThis.foo.
ES6/ES2015 does not account for the Window/WindowProxy structure, and simply refers to ”the global object” directly. This specification does the same. If the ECMAScript specification is changed for top-level this to account for WindowProxy, then the change should also apply to the definition of this proposal.
SES interaction
For Secure ECMAScript, it is important that all references to the global object be spoof-able and capable of being locked down, so that each context gets its own shadow global context. Additionally, references to the global object should not be reachable from other ECMAScript intrinsic objects, which SES would like to simply recursively freeze. In this proposal, globalThis is a writable and configurable property of the global object, so it should meet SES requirements.
Naming
There is desire to reify one of the existing global property names, particularly global or window, instead of System.global. Further research will be done to determine if either of these two options will or will not break existing code doing runtime environment detection.Further research has determined that using Attempts were made to ship under the name global will not break existing code.global, but it turns out that this does, in fact, break some existing websites.
After some data-gathering to determine web compatibility of a short list of choices, we settled on globalThis, as this name is both highly likely to be web compatible, and also maps to the concept of being the “global this value” (and not the “global object”, per above).
For a more detailed list of constraints, see NAMING.md.
Spec
You can view the spec in markdown format or rendered as HTML.
Thanks
Originally inspired by https://twitter.com/littledan/status/627284720284372992 / http://littledan.github.io/global.html