"use strict";
setTimeout(function() {"use strict";console.log(this)}, 1000);
The 'this' inside functions called by setTimeout should refer to global object, but I also have "use strict"; in the body. Yet it logs window instead of undefined, which is what I was expecting. What's happening here?
setTimeout
is defined as calling the function it is passed in the context of window
.
the object on which the method for which the algorithm is running is implemented (a Window or WorkerGlobalScope object) as the method context,
It is akin to calling yourfunction.apply(window)
not yourfunction()
.
Now it's almost 4 years after the question.
There has been a note in MDN setTimeout page, which precisely addresses this question.
The default
this
value of asetTimeout
callback willstill
be thewindow
object, and notundefined
, even in strict mode.
This applies to setInterval
too, even though it is not mentioned there.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With