let CreatePeople = (function(){
let instance = null
return class {
constructor(name,age){
if(instance) return instance
this.name = name
this.age = age
return instance = this
}
}
})()
let p1 = new CreatePeople("小明",18)
let p2 = new CreatePeople("小花",16)
console.log(p1)
console.log(p2)
console.log(p1===p2)
// 封装
let getSingleClass = function(customClass) {
let instance = null
return class {
constructor(){
if(instance) return instance
instance = new customClass(...arguments);
return instance
}
}
}
//使用
class CreateGirlFriend {
constructor(name,age){
this.name = name
this.age = age
}
}
let CreateSingleGirl = getSingleClass(CreateGirlFriend);
let g1 = new CreateSingleGirl("小红",17)
let g2 = new CreateSingleGirl("小率",18)
console.log(g1)
console.log(g2)
console.log(g1 === g2)
console