Features of arrow function

  • If there is only one argument, you can omit the parentheses.
  • If the function body has only one line, you can omit the curly braces.
  • Arrow functions have no arguments variable;
  • It doesn’t change the this point

The arrow function does not have its own this, resulting in the inner this being the this of the outer code block

The function body contains only one line without curly braces

let getTempItem = id =>({id:id,name:'temo'})
console.log(getTempItem(20))
Copy the code

This points to the

  • The normal function this is specified when used, pointing to the caller
  • The arrow function this points to this of the outer function
Var person = {name:'Evan', age:18, Function (){console.log(this)}} person.func() {console.log(this)}} person.func() var person = {name:'Evan', Age :18, func() =>{console.log(this)}} person.func() //this points to the window objectCopy the code

In strict mode this refers to undefined

function f2(){ "use strict"; Return this; } f2() === undefined; // trueCopy the code

SetTimeout & setInterval this

var id = 21; function foo(){ setTimeout(function(){ console.log('id:',this.is); },1000) } foo.call({id:52}); // id:21 Internal cannot call,bind this to change foo(); // id:21 function foo2(){ setTimeout(()=>{ console.log('id:',this.is); },1000) } foo2.call({id:42}); // id:42Copy the code