TIL globalThis
POSTED ON:
TAGS: javascript es2020
Via ES2020, globalThis
is brand new.
To access the global object:
-
In the browser, the global object refers to
window
. You can also useself
, orframes
-
In web workers, you must use
self
-
In NodeJS, you must use
global
The
this
keyword could be used inside functions running in non–strict mode, butthis
will be undefined in Modules and inside functions running in strict mode. You can also useFunction('return this')()
, but environments that disableeval()
, like CSP in browsers, prevent use ofFunction
in this way.
Via the MDN
globalThis
allows us to always refer to the right global object no matter what type of environment you're working on.
REF:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/globalThis
Related TILs
Tagged: javascript