This question is related to topic here
I was expecting console to log window but it is logging LateBloomer. Please help me understand.
This question is related to topic here
I was expecting console to log window but it is logging LateBloomer. Please help me understand.
Because
window.setTimeout(console.log(this), 1000);
calls console.log(this)
, and passes its return value (undefined
) into setTimeout
, exactly the way foo(bar())
calls bar
and passes its return value into foo
. So the this
that gets logged is the current this
where that line of code is.
Whereas in the linked question, they're doing this:
window.setTimeout(this.declare.bind(this), 1000);
which calls bind
and passes its return value (a bound function) into setTimeout
.