js实现单例模式
实现代码:
1 var single=function(name){ 2 this.name=name; 3 } 4 var proxy=(function(){ 5 var instance; 6 return function(name){ 7 if(!instance){ 8 instance= new single(name); 9 } 10 return instance; 11 } 12 })(); 13 14 var a=new proxy("first"); 15 var b=new proxy("second"); 16 17 console.log(a==b);
结果 当然是true
a.name='first'
b.name='first'