Singleton
只能创建一个实例对象
const Singleton = (function(){
let instance;
function createInstance(){
const object = new Object({name:'brad'});
return object;
}
return {
getInstance:function(){
if(!instance){
instance = createInstance();
}
return instance;
}
}
})();
const instanceA = Singleton.getInstance();
const instanceB = Singleton.getInstance();
console.log(instanceA ===instanceB); //True
//只能创建一个instance