Skip to content

Commit

Permalink
docs(object): edit 对象的扩展运算符
Browse files Browse the repository at this point in the history
  • Loading branch information
ruanyf committed Apr 9, 2022
1 parent e856f9e commit 95e2e79
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions docs/object.md
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,23 @@ foo
// {0: "h", 1: "e", 2: "l", 3: "l", 4: "o"}
```

对象的扩展运算符,只会返回参数对象自身的、可枚举的属性,这一点要特别小心,尤其是用于类的实例对象时。

```javascript
class C {
p = 12;
m() {}
}

let c = new C();
let clone = { ...c };

clone.p; // ok
clone.m(); // 报错
```

上面示例中,`c``C`类的实例对象,对其进行扩展运算时,只会返回`c`自身的属性`c.p`,而不会返回`c`的方法`c.m()`,因为这个方法定义在`C`的原型对象上(详见 Class 的章节)。

对象的扩展运算符等同于使用`Object.assign()`方法。

```javascript
Expand Down

0 comments on commit 95e2e79

Please sign in to comment.