"use strict" VS setTimeout

Boris Zbarsky bzbarsky at mit.edu
Sun Sep 7 18:35:50 PDT 2014

On 9/7/14, 1:29 PM, Andrea Giammarchi wrote:
> I know this is probably W3C land but the following code shows the global
> object

Careful with your use of the word "the".  Your ES5-centric assumptions 
are showing.  ;)

The function passed to setTimeout will be invoked with "this" set to the 
window that was the "this" of the setTimeout call.  Note that setTimeout 
itself is a sloppy function for historicaly reasons, which is why it has 
a non-undefined "this" even if called as a bareword.

Now, and here's where I have a problem with your use of "the": the 
Window that setTimeout is invoked on is NOT necessarily the same as the 
global object of the function.  And the web totally depends (or at least 
depended a few years ago) on not getting the global object of the 
function, but rather the Window setTimeout was called on as the "this" 
in the callback.  We tried changing that at one point in Gecko as an 
experiment, and websites broke.

> This looks like a potential problem when possible passed methods are not
> bound + it looks inconsistent with *"use strict"* expectations.

If your expectations include "never called via .call() with an explicit 
'this' passed in", then the expectations might need adjusting.


More information about the es-discuss mailing list