Skip to main content

class

定义只读属性

class MyClass {
constructor(x) {
Object.defineProperty(this, 'a', {
enumerable: false,
configurable: false,
writable: false,
value: x || 'empty',
})
}
}
let o = new MyClass('Hello')

定义私有属性

通过 symbol 或者不放在类中,通过 call 改变 this 执行

const bar = Symbol('bar')
const snaf = Symbol('snaf')

export default class myClass {
// 公有方法
foo(baz) {
this[bar](baz)
}

// 私有方法
[bar](baz) {
return (this[snaf] = baz)
}

// ...
}
class a{
//公有方法
foo(c){
cccc.call(this, c);
}
}

//当前模块的私有方法
function cccc(c){
return this.data = c;
}```