This is a special keyword that is automatically defined in the scope of the function. Actually learning this reminds me of the time when I studied attributive clauses.
1. Why Use this
You can implicitly pass a reference to an object
2. When a function is called, a new execution context is created and the execution context is divided into two stages.
Create phase: Create variable object, establish scope chain, determine this point
So this depends on the execution context, which is how the function is called. Confirm this point to find the function call location.
3. Binding rules
Find the call location and determine which of the four rules.
(1). Default bindings
You can see this pointing to the global variable, window.foo ();
Strict mode, unable to bind by default, can cause typeerror.
(2). Implicit binding considers whether the call location has a context object, or whether it is contained or owned by an object
Obj contains foo,this point to obj, and only the last layer affects the call location.
***
Implicit binding loses an object
The back function modifies this
(3). Explicit binding
Call ()
Apply ()
Foo.call (obj) hard bind, no loss of bound object
(4) New
NEW: Create an object, change this point, connect to the prototype chain, bind the method property to this, and return this
This is detailed in JavaScript