- From: Mark Miller <erights@gmail.com>
- Date: Sun, 22 Feb 2015 13:24:09 -0800
- To: Domenic Denicola <d@domenic.me>
- Cc: "es-discuss@mozilla.org" <es-discuss@mozilla.org>, "public-script-coord@w3.org" <public-script-coord@w3.org>
- Message-ID: <CAK5yZYjMtOK+NHZBiJO91pfw-F=Od-cMBx8Br-ibZNYTz6B_bA@mail.gmail.com>
On Sun, Feb 22, 2015 at 1:03 PM, Domenic Denicola <d@domenic.me> wrote:
> (Cc'ing public-script-coord in case this answer gets more complicated in
> the presence of the window proxy/overriden this-in-global setup.)
>
> Given code like
>
> <script>
> addEventListener("foo", function () { });
> </script>
>
> Is this supposed to be an Invoke(current global, "addEventListener",
> <"foo", function () {}>)?
>
> Or is it just a Call(addEventListener, undefined, <"foo", function () {}>)?
>
> What about if I added "use strict"; to the top of the script?
>
If it is strict code, then this is definitely a
Call(addEventListener, undefined, <"foo", function () {}>)
I won't try to speak definitively for what happens if the code above is
sloppy. But I believe the answer is the same. If the receiver is sloppy, it
is up to it to promote an undefined this-binding to its realm's global
object. As a builtin, it is neither strict nor sloppy, and its spec should
state what it does with an undefined this-binding.
>
> My suspicion was Invoke, but then I came up with another example that
> threw me off:
>
> <script>
> window.globalFunc = function () {
> console.log("this", this);
> };
> </script>
>
> <script>
> "use strict";
> globalFunc();
> </script>
>
> This gives `undefined`. (The result is the same if I also prepend "use
> strict"; to the top of the first script.)
It is quite bizarre that you also get undefined when you don't prepend "use
strict" to the first script. globalFunc should then be sloppy. A sloppy
function should never see its "this" bound to undefined, or indeed to any
non-object value. I do not understand what might be going on here.
Regarding the second script, since you're calling globalFunc as a function
from strict code, you are passing a this-binding of undefined, even though
globalFunc is a global function.
> That nudges me toward the Call answer.
>
> But under the Call answer, my first example would be broken since the
> implementation of `addEventListener` would not have a sensible `this` to
> operate on.
>
If addEventListener was sloppy, it would promote the this-binding to the
global of the realm it was defined in. As a builtin, it is neither strict
nor sloppy, and its spec should state what it does with an undefined
this-binding.
>
> Anyway, I'm clearly missing something. What is it, and where are the
> relevant parts of the spec?
>
I remember where they were in ES5. I am also curious where to find this in
ES6.
--
Cheers,
--MarkM
Received on Sunday, 22 February 2015 21:24:36 UTC