this keyword is ubiquitous yet misconceptions abound.
What you need to know
Every execution context has an associated
ThisBinding whose lifespan is equal to that of the execution context and whose value is constant. There are three types of execution context: global, function and evaluation. Here’s a tabular summary followed by a little more detail, and some examples:
|Execution Context||Syntax of function call||Value of this|
|Global||n/a||global object (e.g.
|Function||Baseless function call:
||global object (e.g.
|Function||Constructor with new:
||the new instance
1. Global context
this is bound to the global object (
window in a browser)