mirror of
https://github.com/ruanyf/es6tutorial.git
synced 2025-05-24 18:32:22 +00:00
docs(class): edit 私有属性
This commit is contained in:
parent
456bfa0fba
commit
46981f6b9a
@ -776,7 +776,16 @@ export default class myClass{
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
上面代码中,`bar`和`snaf`都是`Symbol`值,导致第三方无法获取到它们,因此达到了私有方法和私有属性的效果。
|
上面代码中,`bar`和`snaf`都是`Symbol`值,一般情况下无法获取到它们,因此达到了私有方法和私有属性的效果。但是也不是绝对不行,`Reflect.ownKeys()`依然可以拿到它们。
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
const inst = new myClass();
|
||||||
|
|
||||||
|
Reflect.ownKeys(myClass.prototype)
|
||||||
|
// [ 'constructor', 'foo', Symbol(bar) ]
|
||||||
|
```
|
||||||
|
|
||||||
|
上面代码中,Symbol 值的属性名依然可以从类的外部拿到。
|
||||||
|
|
||||||
### 私有属性的提案
|
### 私有属性的提案
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user