What is this
The execution context contains the variable environment, lexical environment, external environment, and this. Execution contexts have global execution contexts and function execution contexts.
This: in the global execution context refers to the window object.
This in function execution context: By default, a function is called that refers to the window object. How do I change the direction of this?
2. What are the methods to change this
1, call, bind, apply change the direction of this
//apply func.apply(thisArg, [argsArray]) //call fun.call(thisArg, arg1, arg2, ...) //bind const newFun = fun.bind(thisArg, arg1, arg2, ...) NewFun () / / the apply and the call is the different, but both are executed at the time of call and call function, / / but bind will return a binding for this function.Copy the code
2. When the object calls an internal method, this refers to the object itself
Var myObj = function(){console.log(this)}} myobj.showthis ()Copy the code
Three, pay attention
1. The arrow function does not have this; it uses this of the outer function
2. This will not inherit
3. Look for more interview questions about this