箭头函数(arrow function)

2017-07-02  本文已影响0人  阿莱_1b6f

基本语法

(param1, param2, …, paramN) => { statements }
(param1, param2, …, paramN) => expression
// 等价于: (param1, param2, …, paramN) => { return expression; }

// 当参数只有一个的时候,括号是可选的,以下两行等价:
(singleParam) => { statements }
singleParam => { statements }

// 当函数没有参数的时候,需要两个括号.
() => { statements }

高级语法

// Parenthesize the body of function to return an object literal expression:*params* => ({*foo: bar*})
// [Rest parameters](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters) and [default parameters](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Default_parameters) are supported(*param1*, *param2*, **...rest**) => { *statements* }(*param1* **= defaultValue1**, *param2*, …, paramN **= defaultValueN**) => { *statements* }// [Destructuring](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment) within the parameter list is also supportedlet f = ([a, b] = [1, 2], {x: c} = {x: a + b}) => a + b + c;f(); // 6
上一篇 下一篇

猜你喜欢

热点阅读