When a function is called without an associated object, the value of this refers to the global object.
In a web browser, the global object is the window object.
This example returns the window object as the value of this:
let x = myFunction(); // x will be the window object function myFunction() { return this; } |
Invoking a function as a global function sets the value of this to the global object. Using the window object as a variable can easily lead to program crashes. |