Skip to content

Commit a342ebf

Browse files
committed
docs(class): fix private field #1159
1 parent 7dbca2b commit a342ebf

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

docs/class.md

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -844,7 +844,7 @@ class C {
844844
}
845845
```
846846

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

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

@@ -853,12 +853,21 @@ class A {
853853
#foo = 0;
854854
m() {
855855
console.log(#foo in this); // true
856-
console.log(#bar in this); // false
857856
}
858857
}
859858
```
860859

861-
注意,判断私有属性时,`in`只能用在类的内部。
860+
注意,判断私有属性时,`in`只能用在类的内部。另外,判断所针对的私有属性,一定要先声明,否则会报错。
861+
862+
```javascript
863+
class A {
864+
m() {
865+
console.log(#foo in this); // 报错
866+
}
867+
}
868+
```
869+
870+
上面示例中,私有属性`#foo`没有声明,就直接用于`in`运算符的判断,导致报错。
862871

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

0 commit comments

Comments
 (0)