From 6a10936d64141e161f62aff8ce37fb7749fdd24c Mon Sep 17 00:00:00 2001 From: ruanyf Date: Sat, 10 Nov 2018 17:59:52 +0800 Subject: [PATCH] =?UTF-8?q?docs(object):=20edit=20=E6=89=A9=E5=B1=95?= =?UTF-8?q?=E8=BF=90=E7=AE=97=E7=AC=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/array.md | 12 ++++++++++++ docs/object.md | 10 +++++++++- docs/reference.md | 1 + 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/docs/array.md b/docs/array.md index 2f12181..5c0917b 100644 --- a/docs/array.md +++ b/docs/array.md @@ -58,6 +58,18 @@ const arr = [ // [1] ``` +注意,扩展运算符如果放在括号中,JavaScript 引擎就会认为这是函数调用,否则就会报错。 + +```javascript +(...[1,2]) +// Uncaught SyntaxError: Unexpected number + +console.log((...[1,2])) +// Uncaught SyntaxError: Unexpected number +``` + +上面两种情况都会报错,因为扩展运算符所在的括号不是函数调用,而`console.log(...[1, 2])`就不会报错,因为这时是函数调用。 + ### 替代函数的 apply 方法 由于扩展运算符可以展开数组,所以不再需要`apply`方法,将数组转为函数的参数了。 diff --git a/docs/object.md b/docs/object.md index ddb525b..981f320 100644 --- a/docs/object.md +++ b/docs/object.md @@ -563,7 +563,15 @@ let n = { ...z }; n // { a: 3, b: 4 } ``` -这等同于使用`Object.assign`方法。 +由于数组是特殊的对象,所以对象的扩展运算符也可以用于数组。 + +```javascript +let foo = { ...['a', 'b', 'c'] }; +foo +// {0: "a", 1: "b", 2: "c"} +``` + +对象的扩展运算符等同于使用`Object.assign()`方法。 ```javascript let aClone = { ...a }; diff --git a/docs/reference.md b/docs/reference.md index 0797764..a83cac2 100644 --- a/docs/reference.md +++ b/docs/reference.md @@ -103,6 +103,7 @@ - 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) - 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