.bind
The first parameter for bind
is the value of this
inside the binded function. Passing window
(or null
, that would be equivalent) is a way to attach this
to the global scope.
setTimeout
setTimeout
is a method of window
(window.setTimeout
), then this
inside that function should be window
, but it is not needed in current browsers.
Remember that setTimeout(...)
is equivalent to call window.setTimeout(...)
because window
is the global context.
Who is this
?
An example:
var foobar = window.setTimeout;
This code could not work the way you would expect, it depends on the context, that we do not know.
For window.setTimeout
, this
would be the contextual this
of this assignment. To force the reference to another this
, you have to set the reference with bind
:
var foobar = window.setTimeout.bind(window);
Except when assigning it from the global context, in that case the function will have this
as window
.
Your code without binding
In your exact case, if you do do not bind and just assign:
setTimerFunctions: SetTimerFunctions = {
setInterval: setInterval,
setTimeout: setTimeout,
}
then this
, from the setTimeout
/setInterval
point of view, will be every instance of the class where this code is declared (as it seems to be part of a class declaration in TypeScript).
Extra: function vs arrow function
Take a look at the documentation and differences between function
and arrow functions =>
, this issue is one of the reasons why arrow functions were added.