this
Any access to this
keyword within a function is controlled by how the function is actually called. It is commonly referred to as the “calling context.”
Here is an example:
[[Error_TS2683]]
function foo() {
console.log(this);
}
> Output:
error TS2683: 'this' implicitly has type 'any' because it does not have a type annotation.
foo(); // logs out the global e.g. `window` in browsers
let bar = {
foo
}
bar.foo(); // Logs out `bar` as `foo` was called on `bar`
So be mindful of your usage of this
. If you want to disconnect this
in a class from the calling context use an arrow function, more on that later.