globalThis
is quite new. It's not supported in Edge yet. It was created to workaround the fact if you wrote JS for multiple environments ( like a server and the browser ) they used different names for the global object.
From MDN:
Historically, accessing the global object has required different syntax in different JavaScript environments. On the web you can use window, self, or frames - but in Web Workers only self will work. In Node.js none of these work, and you must instead use global.
The 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.
The 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.
I think the C3 runtime will create globalThis
if it doesn't exist. But it depends where you're reading it from I guess.
If you're only doing this in the browser it makes sense to just use window
/frame.contentWindow
.
Edge is being replaced with a Chromium based version very soon, so it will have support for it then.