- 使用Symbol实现
私有属性
const _radius = Symbol()
class Circle {
constructor(radius) {
this[_radius] = radius
}
}
const c = new Circle(1)
const key = Object.getOwnPropertySymbols(c)[0]
console.log(c[key])
私有方法
const _radius = Symbol()
const _draw = Symbol()
class Circle {
constructor(radius) {
this[_radius] = radius
}
[_draw]() {
console.log('draw')
}
}
const c = new Circle(1)
- 使用weakMap实现
私有变量