Context
The keyword this
refers to the "current execution context", or just the "context" for short.
We mostly use this
in classes — its behavior is typically not helpful and somewhat error-prone elsewhere.
Global this
Outside of any function, the current execution context is an environment-specific global variable (window
in the browser, global
in node).
Function this
Inside a function declared with function
, this
refers to an arbitrary value set by the caller.
Inside a function declared with =>
, this
refers to whatever it was in the parent scope.
We can specify the this
value with .call()
.
Automatic this
Functions that are properties of objects will have their this
set to the object when called with .
syntax. This applies to instances of classes too.
This is the main use case for this
.