self is a cross-platform way to name a window
Written
While scrolling through the Deno 2.0 release notes one thing cought mine eye: deprecation of window.
My first suprise was that this even existed - why standalone JavaScript interpreter needs a window?
And the second one was: what is globalThis and self?
Naming global context
| Browser | WebWorker | Nodejs | Deno | Bun | |
|---|---|---|---|---|---|
| window | ✓ | ✓ | |||
| frames | ✓ | ||||
| self | ✓ | ✓ | ✓ | ✓ | ✓ |
| globalThis | ✓ | ✓ | ✓ | ✓ | |
| global | ✓ | ✓ |
There is only one way to access in all JavaScript execution environments the global object: self.
Any other way is quirky and works only in some context.
Standards
| HTML Spec | The window, frames, and self getter steps are to return this's relevant realm.[[GlobalEnv]].[[GlobalThisValue]]. |
|---|---|
| ECMAScript 2020 | The initial value of the "globalThis" property of the global object in a Realm Record realm is realm.[[GlobalEnv]].[[GlobalThisValue]]. |
ECMAScript doesn't have self defined in the standard and the HTML standard doesn't have globalThis defined, relying on reference to ECMA-262 standard.
So the only cross platform convention is unofficial one.