Why is the window still defined in this strict mode code?

"use strict";

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

The internal 'this' functions called by setTimeout must reference the global object, but I also have "use strict"; in organism. However, it writes a window instead of undefined, which I expected. What's going on here?

+3
source share
3 answers

setTimeoutdefined as a call to the function that it passed in context window.

an object on which the method is implemented, for which the algorithm is executed (Window object or WorkerGlobalScope) as the context of the method,

This is like calling yourfunction.apply(window)not yourfunction().

+7
source

Now this is almost 4 years after the question.

MDN setTimeout , .

this setTimeout still window, undefined, .

setInterval, .

+3

because it is actually:

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

you call setTimeout with the object window, even though you are not writing it.

+1
source

All Articles