"use strict" VS setTimeout

Garrett Smith dhtmlkitchen at gmail.com
Sun Sep 7 12:50:56 PDT 2014


On 9/7/14, Andrea Giammarchi <andrea.giammarchi at gmail.com> wrote:
> this is getting nowhere ... yeah Garret, you can use `.call` and we all
> know that ...
>
> Now I want you to answer this: why on earth would you expect a global
> context in a setTimeout or setInterval operation for a function/method you
> have explicitly defined as strict ?
>
> One single use case ... do you have it ?
>
> 'cause you don't use "use strict" inside method/functions passed to
> `addEventListener` as example, do you?
>
> So I will close the loop with the very initial snippet and just one extra
> comment
>
> ```js
> (function () {
>   'use strict'; // <== as a developer, I don't want implicit window
>   setTimeout(function () {
>     'use strict'; // <== as a developer, I don't want implicit window
>     console.log(this);
>     // [window/global Object]
>     // SO WHY ON EARTH I HAVE AN IMPLICIT window HERE ?
>   }, 0);
> }());
> ```
>

setTimeout is explicitly calling your callback with the window. It's
specified to do so in HTML5. It is a little strange. Should HTML5 be
changed?

http://www.whatwg.org/specs/web-apps/current-work/multipage/webappapis.html#dom-windowtimers-settimeout
-- 
Garrett
@xkit
ChordCycles.com
garretts.github.io


More information about the es-discuss mailing list