Skip to content

Commit

Permalink
docs(class): fix private field #1159
Browse files Browse the repository at this point in the history
  • Loading branch information
ruanyf committed Jul 13, 2023
1 parent 8afac58 commit d9183de
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions docs/class.md
Original file line number Diff line number Diff line change
Expand Up @@ -844,7 +844,7 @@ class C {
}
```

上面示例中,`in`运算符判断某个对象是否有私有属性`#foo`。它不会报错,而是返回一个布尔值。
上面示例中,`in`运算符判断某个对象是否有私有属性`#brand`。它不会报错,而是返回一个布尔值。

这种用法的`in`,也可以跟`this`一起配合使用。

Expand All @@ -853,12 +853,21 @@ class A {
#foo = 0;
m() {
console.log(#foo in this); // true
console.log(#bar in this); // false
}
}
```

注意,判断私有属性时,`in`只能用在类的内部。
注意,判断私有属性时,`in`只能用在类的内部。另外,判断所针对的私有属性,一定要先声明,否则会报错。

```javascript
class A {
m() {
console.log(#foo in this); // 报错
}
}
```

上面示例中,私有属性`#foo`没有声明,就直接用于`in`运算符的判断,导致报错。

子类从父类继承的私有属性,也可以使用`in`运算符来判断。

Expand Down

0 comments on commit d9183de

Please sign in to comment.