"use strict" VS setTimeout

Mark Miller erights at gmail.com
Sun Sep 7 11:07:42 PDT 2014


On Sun, Sep 7, 2014 at 10:29 AM, Andrea Giammarchi <
andrea.giammarchi at gmail.com> wrote:

> I know this is probably W3C land but the following code shows the global
> object in every JS engine I could test:
>
> ```js
> (function () {
>   'use strict';
>   setTimeout(function () {
>     'use strict';
>     console.log(this);
>     // [window/global Object]
>   }, 0);
> }());
> ```
>

On Sun, Sep 7, 2014 at 11:02 AM, Axel Rauschmayer <axel at rauschma.de
<https://mail.google.com/mail/?view=cm&fs=1&tf=1&to=axel@rauschma.de>>
wrote:

> On Sep 7, 2014, at 19:47 , Mark S. Miller <erights at google.com
> <https://mail.google.com/mail/?view=cm&fs=1&tf=1&to=erights@google.com>>
> wrote:
>
> On Sun, Sep 7, 2014 at 10:36 AM, Mathias Bynens <mathiasb at opera.com
> <https://mail.google.com/mail/?view=cm&fs=1&tf=1&to=mathiasb@opera.com>>
> wrote:
>
>> On Sun, Sep 7, 2014 at 7:29 PM, Andrea Giammarchi
>> <andrea.giammarchi at gmail.com
>> <https://mail.google.com/mail/?view=cm&fs=1&tf=1&to=andrea.giammarchi@gmail.com>>
>> wrote:
>> > This looks like a potential problem when possible passed methods are not
>> > bound + it looks inconsistent with *"use strict"* expectations.
>>
>
> Yes. This looks like a typical screwup. Thanks for pointing it out.
>
>
> Interesting. Follow-up question: isn’t strictness propagated lexically?
>

Yes.



> That is, shouldn’t the parameter of `setTimeout()` be strict even without
> being explicitly declared as such?
>

Yes, it is. That's what demonstrates that this is a W3C land screwup, as
Andrea inferred, rather than a JS issue. The callback is strict. It is
setTimeout itself which is explicitly passing it the global as a
this-binding, rather than passing it undefined. If the callback function
were sloppy, it would see the global object in either case.



-- 
  Cheers,
  --MarkM
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20140907/1592b6ad/attachment.html>


More information about the es-discuss mailing list