4

In strict mode, this should be undefined inside of a non-method (a function):

see http://jsfiddle.net/jfp06nc9/1/
showing this is undefined

However, when the setTimeout is used, then this is bound to window:

see http://jsfiddle.net/jfp06nc9/2/
and http://jsfiddle.net/jfp06nc9/3/
showing that this === window returns true.

so it looks like the function fn passed to setTimeout is invoked not as a function, but as a method, like window.fn() or fn.call(window) or (fn.bind(window))().

see http://jsfiddle.net/jfp06nc9/4/
showing the last 3 calls above would all show this === window as true.

Is that true? I can't find any specification about it so please include that in your answer.

(the jsfiddle was running in Chrome 46.0)

P.S. This question is NOT about what this is bound to, but about, in non-strict mode, setTimeout can run fn() and the this will be bound to window. But in strict mode, if setTimeout really runs it as fn(), then this should now be undefined instead of window, so there is a subtle difference there, that it seems setTimeout runs it (or arrange to run it) not as fn() but as fn.call(window)

The jsfiddle and code:

http://jsfiddle.net/jfp06nc9/7/

(function() {
    "use strict";
    setTimeout(function() {
        "use strict";
        console.log(this); 
    }, 1000);
}());

Since the function runs in strict mode, if it is run as a function, then this should be undefined but it is in fact window

nonopolarity
  • 146,324
  • 131
  • 460
  • 740
  • Please add the relevant code to the question, not in fiddles. – Andy Nov 20 '15 at 16:29
  • 3
    MDN [does explain this](http://mdn.beonex.com/en/DOM/window.setTimeout.html#The_%27this%27_problem) in their documentation for `setTimeout()`. – Frédéric Hamidi Nov 20 '15 at 16:29
  • Possible duplicate of [Pass correct "this" context to setTimeout callback?](http://stackoverflow.com/questions/2130241/pass-correct-this-context-to-settimeout-callback) – dsh Nov 20 '15 at 16:29
  • Yes , it is true, windows has a lot of functions like alert, setTimeout, setInterval etc. https://developer.mozilla.org/en-US/docs/Web/API/Window that is why you are getting this as window object – Zohaib Ijaz Nov 20 '15 at 16:30
  • Your question has, really, nothing to do with `setTimeout()`, but it has everything to do with how `this` works in Javascript. It is different from other languages like Java, C++ and Python. [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this) [explanation on Quirksmode](http://www.quirksmode.org/js/this.html) – dsh Nov 20 '15 at 16:31
  • why would he ask if he know that setTimeout() has nothing to do with `this` ... – Zohaib Ijaz Nov 20 '15 at 16:34
  • @dsh isn't it true that I can implement a `setTimeout2` or `delayThis` that takes a function, and call it as a function, not as a method? (meaning that the behavior is up to the function, in this case, up to `setTimeout` – nonopolarity Nov 20 '15 at 16:37

1 Answers1

3

setTimeout is part of the HTML spec (as opposed to ECMAScript), and it does indeed clarify that window should be used for this.

The relevant part of the spec is as follows:

  1. Let method context proxy be method context if that is a WorkerGlobalScope object, or else the WindowProxy that corresponds to method context.

(emphasis mine)

along with:

4.2 Run the appropriate set of steps from the following list:

If the first method argument is a Function

Call the Function. Use the third and subsequent method arguments (if any) as the arguments for invoking the Function. Use method context proxy as the thisArg for invoking the Function.

(emphasis mine)

Community
  • 1
  • 1
James Thorpe
  • 31,411
  • 5
  • 72
  • 93