Under ES6 new features [front-end]

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/cheidou123/article/details/91649944

A function arrow

Function defined by the arrow => symbol.

1. Basic Usage

Arrow function equivalent anonymous function, so the use of functions written expression.

  • ⑴ basic usage


  • ⑵ only one parameter


  • ⑶ no parameters
    if no parameter directly () will do
    the ordinary functions of writing:
2. Considerations
  • ⑴ arrow in the function of this points to an object defined function is located, rather than where the object is running
let json = {
    name: '苏日俪格',
    show: function(){
        setTimeout(function(){
            console.log(this.name); // 控制台什么都没有输出
        },1000);
    }
}
json.show();

let json = {
    name: '苏日俪格',
    show: function(){
        setTimeout(() => {
            console.log(this.name); // 苏日俪格
        },1000);
    }
}
json.show();
  • ⑵ arrow inside the function can not use arguments, if that is not defined by the
  • ⑶ arrow function can not be used as a constructor, write the following is wrong:
let Show = ()=>{
    this.name = '苏日俪格';
}
let a = new Show();
console.log(a.name);    // Show is not a constructor

Guess you like

Origin blog.csdn.net/cheidou123/article/details/91649944