This spec introduces self
, a writable, configurable property of the global object which has the global object as its value. This name should be familiar for developers, as it is already available as part of HTML, but this proposal brings it into availability for all ECMAScript code.
It is difficult to write portable ECMAScript code which accesses the global object. On the web, it is accessible as window
or self
or frames
; on Node.js, it is global
; neither of those is available in a shell like V8's d8. In sloppy mode, it is this
; in strict code within a function, that can still be accessed by new Function('return this')
, but that form is inaccessible with some CSP settings. 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 strict mode inside a function (it could be fixed using the new Function
trick):
// If we're in a browser, the global namespace is named 'window'. If we're // in node, it's named 'global'. If we're in a shell, 'this' might work. })(typeof window !== "undefined" ? window : (typeof process === 'object' && typeof require === 'function' && typeof global === 'object') ? global : this);
In HTML5, 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, the global object maintains the same identity while the underlying Window
is swapped out.
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 self
.
self
is a writable, configurable property of the global object, so it should meet SES requirements.
The initial value of the "self"
property of the global object is the global object.
This property has the attributes {[[Writable]]: