what do you do if you want to reference "this" in the anonymous inner function at some later time?
There is no way to refer to the value of this
that the callback would have if we hadn't called .bind
.
However, you don't have to use .bind
, there are other ways to use the this
value of the "parent" function: How to access the correct `this` context inside a callback?
Does "this" not exist for the anonymous inner function?
this
is an implicit value in every function. Which value it has is determined by how the function is called, unless the function is explicitly bound to a certain value (via .bind
).
For example, if you call a function "normally", such as func()
, then this
will refer to the global object (window
in browsers). The API documentation usually explains which value this
has inside a callback. If it doesn't mention anything explicitly, then you can assume that this
refers to the global object, which is not very useful in most cases, and thus it is no problem to "override" it via .bind
.
But even if this
is set to a specific value in the callback, that value is often also passed as argument to the callback. Being able to access the value via this
is just for convenience. But again, it depends on how the API is designed and is usually explained in the API documentation.
For more information about this
, have a look at the MDN documentation.