单例模式(改造)
作者:互联网
/*
单例改造
1.需要instance 保存下来
*/
const singleTon=(function(){
function Person(name,age,sex){
this.name = name;
this.age = age;
this.sex = sex;
}
Person.prototype.say = function(){
console.log('hello')
}
let instance = null;
return function singleTon(...age){
if(!instance) instance = new Person(...age)
return instance
}
})()
const p1 = singleTon('熊明',18,'男')
const p2 = new singleTon('徐奥话',20,'女')
console.log(p1,p2)
console.log(p1==p2)
标签:function,singleTon,const,改造,age,模式,sex,instance,单例 来源: https://blog.csdn.net/weixin_42327537/article/details/123240058