使用 symbol唯一性 + 块级作用域
var class1;
{
let x=Symbol('qwdw');
let y='xx1';
class1 = class Txx1{
constructor(){
console.log('const');
this[x]();
console.log('const2');
}
[x](){
console.log('start');
console.log(this);
console.log('end');
}
[y](){
console.log(this);
}
}
}
使用属性特性 + 返回错误提示
class T {
y=1;
constructor(){
Object.defineProperty(this,'y',{
enumerable:false,
writable:false,
value:0
});
}
x(){
if(this.y && new.target){
//.....
}else{
throw Error('私有方法');
}
}
}
闭包
class T{
constructor(){
function x(){
console.log('哈哈')
}
return new class{
constructor(){
x();
}
}
}
}