1
0
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:
ruanyf 2019-01-21 00:41:13 +08:00
parent 456bfa0fba
commit 46981f6b9a

View File

@ -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 值的属性名依然可以从类的外部拿到。
### 私有属性的提案