Skip to content

Commit

Permalink
Update es6 arrow function
Browse files Browse the repository at this point in the history
  • Loading branch information
wwsun committed Nov 13, 2015
1 parent 743e7e2 commit 31d2758
Showing 1 changed file with 12 additions and 21 deletions.
33 changes: 12 additions & 21 deletions README.md
Expand Up @@ -1384,36 +1384,27 @@

## ES6箭头函数

- 所有的Arrow Function的参数均使用 `()`包裹,即便只有一个参数:
- 当你必须使用函数表达式(或传递一个匿名函数时),使用箭头函数符号(能够自动绑定`this`到父对象)

```javascript
// good
let foo = (x) => x + 1;

// bad
let foo = x => x + 1;
```
[1, 2, 3].map(function (x) {
return x * x;
});

- 定义函数尽量使用Arrow functions,而不是`function`关键字

This comment has been minimized.

Copy link
@DMY-sunny

DMY-sunny Nov 23, 2017

functions也是一个函数命名的方法吗

// good
[1, 2, 3].map((x) => x * x);
```

- 建议所有的Arrow Function的参数均使用 `()`包裹,即便只有一个参数:

```javascript
// good
let foo = () => {
// code
};

// bad
function foo() {
// code
}
let foo = (x) => x + 1;

// bad
let foo = function () {
// code
}
```

除非当前场景不适合使用Arrow Function,如函数表达式需要自递归、需要运行时可变的`this`对象等。
let foo = x => x + 1;
``

- 对于对象、类中的方法,使用增强的对象字面量

Expand Down

0 comments on commit 31d2758

Please sign in to comment.