"use strict" VS setTimeout

Andrea Giammarchi andrea.giammarchi at gmail.com
Sun Sep 7 12:03:08 PDT 2014


Garret for legacy we are good to go with `.call(undefined)` that will bring
window in there but `addEventListener` is a different intent/operation than
`setTimeout` plus you lways have `e.currentTarget` which is always
preferable anyway since this will not produce what you expect:

```js
addEventListener("click", {handleEvent: function(ev){
  console.log(this === window); // nope
}});
```

Regards


On Sun, Sep 7, 2014 at 7:59 PM, Garrett Smith <dhtmlkitchen at gmail.com>
wrote:

> On 9/7/14, Mark Miller <erights at gmail.com> wrote:
> > On Sun, Sep 7, 2014 at 11:27 AM, Domenic Denicola <
> > domenic at domenicdenicola.com> wrote:
> >
> >>  I don't understand why this is any more surprising than any other
> >> function that calls its callback with .call(something).
> >>
> >
> > The issue is what the something should be, and which choices for
> something
> > are surprising for what APIs.
> >
> >
> >
> >> It doesn't matter whether the callback is strict or not; .call(window),
> >> which is what the spec does, will override it.
> >>
> >
> > I don't understand what you're trying to say here. What will override
> what?
> >
> >
> >>
> >> As far as I can see this issue has absolutely nothing to do with strict
> >> vs. sloppy.
> >>
> >
> > As Andrea initially stated, this is a DOM-spec issue, not a JS issue, in
> > that JS allows setTimeout to call the callback with WTF it wants.
> However,
> > the purpose of setTimeout, etc, from the JS programmer's perspective, is
> to
> > postpone some action to some future time. From this understanding,
> there's
> > no reason to give the postponed action access to the global object. From
> a
> > POLA perspective, there is thus strong reason not to.
> >
>
> If legacy compatibility is needed then setTimeout's callback function
> must be called with the same window of setTimeout, just as with global
> addEventListener.
>
> <script>
> "use strict"
> addEventListener("click", function(ev){
>   console.log(this === window);
> });
> </script>
> --
> Garrett
> @xkit
> ChordCycles.com
> garretts.github.io
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20140907/22821319/attachment-0001.html>


More information about the es-discuss mailing list