ES6 function uses arrow

ES6 allows => defined functions, a more concise expression of the arrow so that the function, e.g.

// 正常函数写法
[1,2,3].map(function (x) {
  return x * x;
});

// 箭头函数写法
[1,2,3].map(x => x * x);

First, define how

1. The line of code block may be omitted braces {}

var f = () => 5;
//等同
var f = function f() {
    return 5
}

2. a parameter of the function may be omitted parentheses ()

var f = v => v;
//等同
var f = function f(v) {
    return v
}

3. Parameter plurality of lines of code written routine

var sum = (num1, num2) => num1 + num2;
// 等同于
var sum = function(num1, num2) {
    return num1 + num2;
};

Precautions

  1. The body of the this object functions, when the pointing object definitions, rather than use the
  2. Not as a constructor can not use the new command
  3. Objects can not use the arguments, parameters may be used in place of rest
  4. Yield not use command, and therefore can not function as an arrow Generator

Guess you like

Origin www.cnblogs.com/chenqionghe/p/11412137.html