2

Could someone please point to the place in the ES2015 standard that states what the this must refer to when used in a strict mode in a global scope?

I have found that it is equal to the window in my firefox and chrome, but is not equal to window in my IE11 under windows 7 x64.

The only relevant place in the standard I could find is ES2015 - The Strict Mode of ECMAScript but it looks vague and does not state that explicitly.

zerkms
  • 249,484
  • 69
  • 436
  • 539
  • As I mentioned in the comments on another question, IE11 (11.0.10240.16384) on Windows 10 x64 does not appear to exhibit this behavior, so there may be something else going on here as well. The question on what the spec defines is definitely interesting though. – Alexander O'Mara Jan 24 '16 at 08:43
  • 1
    Related question: [`this` in global scope in ECMAScript 6](http://stackoverflow.com/questions/13425333/this-in-global-scope-in-ecmascript-6). – jfriend00 Jan 24 '16 at 08:44
  • 1
    Not sure if this answers the question (it's rather implicit), but the [spec says about `HasThisBinding` says](http://www.ecma-international.org/ecma-262/6.0/#sec-global-environment-records-hasthisbinding): *"Global Environment Records always provide a this binding whose value is the associated global object."*. You can see in other cases (e.g. Function Environment Records) that the logic is more complex (even though it doesn't explicitly mention strict mode). – Felix Kling Jan 24 '16 at 20:32

1 Answers1

0

The ECMAScript spec does not define which object should be the global in particular runtime environments. For browsers this is defined by the HTML5 spec. There is some special-casing for window via the windowproxy object, but that should generally be invisible to javascript.

I.e. even if top level this !== window in a browser script this does not necessarily stem from ecma spec properties or strict mode. It could also stem from from the html spec's definition what a global ought to be for that particular script environment or whether that global actually equals <local variable bindings>.window.


An issue with your question is what you call the global scope. The spec has a concept of global lexical environment. But not all script bodies (source code) are evaluated with a realm's global lexical environment as their lexical environment.

A further complication is that that scripts can not just run in different lexical environments but that there can also be multiple realms which can interact and yet have distict globals.
This may seem like a non-issue because different code realms (e.g. workers or browser windows) mostly interact through small API surfaces, but reality is more complex than that. E.g. one realm can use a global object which has a prototype which is a proxy to a different realm's global. In many ways they will look like sharing a global when they actually are not and doing something like this == this.referenceToSelf may return false. This commonly happens with firefox's sandboxes which are used to run addons/webextensions/userscripts.


A counterexample where a "top level" this is not the global object would be es6 modules.

So there exists strict mode code where this in the top level lexical is not the global object.

A much more trivial example is code eval'd in the context of a function. It inherits the this from the function.

the8472
  • 40,999
  • 5
  • 70
  • 122
  • The question is if it should even refer to a global object or not. It's not about what `window` should be. Sorry, but not an answer. – zerkms Jan 24 '16 at 09:46
  • You still cannot apply the ecma spec in a vacuum. What the lexical environment of a particular script is - and thus its `this` binding - depends on choices made by the runtime, in other words, how html specifies that ` – the8472 Jan 24 '16 at 10:52
  • "you have to take both specs into account" --- I'm not sure how the `this` value can be of HTML standard business. Does it even know anything about it? If so - which standard restricts on the value for `this` in a strict mode? – zerkms Jan 24 '16 at 11:10
  • The issue is that you're asking whether top level this == global. But your question is motivated by top level this == window, in a – the8472 Jan 24 '16 at 11:47
  • "But your question is motivated by top level this == window, in a – zerkms Jan 24 '16 at 21:08
  • Well, if I read the spec correctly then in other environments `this` may be `undefined`. Specifically module environments are defined to have a *this binding* and that this binding is `undefined`. Modules also evaluate in strict mode, therefore some strict mode code may have top level this != global object. – the8472 Jan 25 '16 at 04:06
  • If that (with quotes/references to the spec) is put as an answer that would count :-) – zerkms Jan 25 '16 at 04:20