mirror of
https://github.com/ruanyf/es6tutorial.git
synced 2025-05-24 10:22:23 +00:00
docs(object): edit 扩展运算符
This commit is contained in:
parent
b48ce465ba
commit
6a10936d64
@ -58,6 +58,18 @@ const arr = [
|
|||||||
// [1]
|
// [1]
|
||||||
```
|
```
|
||||||
|
|
||||||
|
注意,扩展运算符如果放在括号中,JavaScript 引擎就会认为这是函数调用,否则就会报错。
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
(...[1,2])
|
||||||
|
// Uncaught SyntaxError: Unexpected number
|
||||||
|
|
||||||
|
console.log((...[1,2]))
|
||||||
|
// Uncaught SyntaxError: Unexpected number
|
||||||
|
```
|
||||||
|
|
||||||
|
上面两种情况都会报错,因为扩展运算符所在的括号不是函数调用,而`console.log(...[1, 2])`就不会报错,因为这时是函数调用。
|
||||||
|
|
||||||
### 替代函数的 apply 方法
|
### 替代函数的 apply 方法
|
||||||
|
|
||||||
由于扩展运算符可以展开数组,所以不再需要`apply`方法,将数组转为函数的参数了。
|
由于扩展运算符可以展开数组,所以不再需要`apply`方法,将数组转为函数的参数了。
|
||||||
|
@ -563,7 +563,15 @@ let n = { ...z };
|
|||||||
n // { a: 3, b: 4 }
|
n // { a: 3, b: 4 }
|
||||||
```
|
```
|
||||||
|
|
||||||
这等同于使用`Object.assign`方法。
|
由于数组是特殊的对象,所以对象的扩展运算符也可以用于数组。
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
let foo = { ...['a', 'b', 'c'] };
|
||||||
|
foo
|
||||||
|
// {0: "a", 1: "b", 2: "c"}
|
||||||
|
```
|
||||||
|
|
||||||
|
对象的扩展运算符等同于使用`Object.assign()`方法。
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
let aClone = { ...a };
|
let aClone = { ...a };
|
||||||
|
@ -103,6 +103,7 @@
|
|||||||
- Axel Rauschmayer, [Enumerability in ECMAScript 6](http://www.2ality.com/2015/10/enumerability-es6.html)
|
- Axel Rauschmayer, [Enumerability in ECMAScript 6](http://www.2ality.com/2015/10/enumerability-es6.html)
|
||||||
- Axel Rauschmayer, [ES proposal: Object.getOwnPropertyDescriptors()](http://www.2ality.com/2016/02/object-getownpropertydescriptors.html)
|
- Axel Rauschmayer, [ES proposal: Object.getOwnPropertyDescriptors()](http://www.2ality.com/2016/02/object-getownpropertydescriptors.html)
|
||||||
- TC39, [Object.getOwnPropertyDescriptors Proposal](https://github.com/tc39/proposal-object-getownpropertydescriptors)
|
- TC39, [Object.getOwnPropertyDescriptors Proposal](https://github.com/tc39/proposal-object-getownpropertydescriptors)
|
||||||
|
- David Titarenco, [How Spread Syntax Breaks Javascript](https://dvt.name/2018/06/02/spread-syntax-breaks-javascript/): 扩展运算符的一些不合理的地方
|
||||||
|
|
||||||
## Symbol
|
## Symbol
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user