mirror of
https://github.com/ruanyf/es6tutorial.git
synced 2025-05-27 20:32:21 +00:00
docs(object): fix object spread #630
This commit is contained in:
parent
3a79d6799e
commit
592ab333d8
@ -62,7 +62,7 @@ Number.isNaN('true' / 0) // true
|
||||
Number.isNaN('true' / 'true') // true
|
||||
```
|
||||
|
||||
注意,如果参数类型不是数值,`Number.isNaN`一律返回`false`。
|
||||
如果参数类型不是`NaN`,`Number.isNaN`一律返回`false`。
|
||||
|
||||
它们与传统的全局方法`isFinite()`和`isNaN()`的区别在于,传统方法先调用`Number()`将非数值的值转为数值,再进行判断,而这两个新方法只对数值有效,`Number.isFinite()`对于非数值一律返回`false`, `Number.isNaN()`只有对于`NaN`才返回`true`,非`NaN`一律返回`false`。
|
||||
|
||||
|
@ -1317,13 +1317,19 @@ o3.a // undefined
|
||||
const o = Object.create({ x: 1, y: 2 });
|
||||
o.z = 3;
|
||||
|
||||
let { x, ...{ y, z } } = o;
|
||||
let { x, ...newObj } = o;
|
||||
let { y, z } = newObj;
|
||||
x // 1
|
||||
y // undefined
|
||||
z // 3
|
||||
```
|
||||
|
||||
上面代码中,变量`x`是单纯的解构赋值,所以可以读取对象`o`继承的属性;变量`y`和`z`是扩展运算符的解构赋值,只能读取对象`o`自身的属性,所以变量`z`可以赋值成功,变量`y`取不到值。
|
||||
上面代码中,变量`x`是单纯的解构赋值,所以可以读取对象`o`继承的属性;变量`y`和`z`是扩展运算符的解构赋值,只能读取对象`o`自身的属性,所以变量`z`可以赋值成功,变量`y`取不到值。ES6 规定,变量声明语句之中,如果使用解构赋值,扩展运算符后面必须是一个变量名,而不能是一个解构赋值表达式,所以上面代码引入了中间变量`newObj`,如果写成下面这样会报错。
|
||||
|
||||
```javascript
|
||||
let { x, ...{ y, z } } = o;
|
||||
// SyntaxError: ... must be followed by an identifier in declaration contexts
|
||||
```
|
||||
|
||||
解构赋值的一个用处,是扩展某个函数的参数,引入其他操作。
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user