this

  • This in the function
    • Explicitly specify who: obj.xxx()
    • Use call/apply to specify who calls: xxx.call(obj)
    • Does not specify who calls: XXX () : window
    • Callback function: see who calls it from behind: window/ other

This in the arrow function

  • The this in the arrow function is bound when the function is defined, not when the function is executed.
  • The this in the arrow function points to fixed, because the arrow function does not have its own this, and the inner this is the this in the outer code block (the first one in the outer code block is not the arrow function).
  • No this, so no constructor, no call, no apply
var x = 111
var obj = {
    x:222,
    say:() =>{
        console.log(this.xxx)
    },
    getValue:function(){
        fn:()=>{
            console.log(this.xxx)
        }
    }
}
obj.say()   
obj.getValue()  
Copy the code
  • Note: say() returns 111, this inherits from this in the parent execution. The arrow function is say, level with obj, and the parent execution context is window
  • GetValue () returns 222 because the arrow function is level with getValue and has a parent of obj

Characteristics of the arrow function

  • The arrow function this is the value of the function scope or global scope closest to the arrow function at runtime
  • Arrow functions cannot be constructors; new cannot be used
  • The arrow function is called through call and apply and does not change the this pointer, but only the arguments
  • Arrow functions have no stereotype attributes
  • Arrow functions have no arguments, caller, callee
  • Arrow functions cannot be Generator functions and cannot use the yield keyword
  • Methods that cannot act as objects