class Singleton {
constructor() {
this.instance = null;
}
// 构造一个广为人知的接口,供用户对该类进行实例化
static getInstance() {
if (!this.instance) {
this.instance = new Singleton();
}
return this.instance;
}
}
var a = Singleton.getInstance();
var b = Singleton.getInstance();
// 指向的是唯一实例化的对象
console.log(a === b)