RE: Global method calls

Thanks Mark. At this point it may tend toward more of a public-script-coord question...

From: Mark Miller [mailto:erights@gmail.com] 

> 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.

The added weirdness here is that addEventListener is actually a method of EventTarget, which Window derives from. (And then, of course, the window proxy masks direct access, at least when you do `window.addEventListener`---but apparently the window proxy is not involved in my case.)

The spec for addEventListener [1] doesn't mention what to do with an undefined `this` binding. Although the language is of the typical imprecise-DOM-spec-type, as far as I can tell it assumes that its `this` is always an `EventTarget` instance, which then has "an associated list of event listeners" it operates on.

At this point I must imagine that there is some special handling taking place somewhere else in the web ecosystem, possibly in WebIDL, that will ensure addEventListener (and possibly any other method?) will use the global window (but not the window proxy?) when called with undefined `this`. I don't know where to find that, though: I looked through [2] without much luck, and Ctrl+Fing for [[Call]] throughout WebIDL does not give anything fruitful.

[1]: https://dom.spec.whatwg.org/#dom-eventtarget-addeventlistener

[2]: https://heycam.github.io/webidl/#Global


> 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.

You are right; please disregard this part. I don't get undefined in the given example---only when adding "use strict" to both.

Received on Sunday, 22 February 2015 21:39:32 UTC