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
- The body of the this object functions, when the pointing object definitions, rather than use the
- Not as a constructor can not use the new command
- Objects can not use the arguments, parameters may be used in place of rest
- Yield not use command, and therefore can not function as an arrow Generator