1 //singleton 单例模式 2 3 import console; 4 5 import thread; 6 class singleton{ 7 ctor(){}; 8 instance; 9 10 test = function(){ 11 return "对象"; 12 } 13 14 } 15 16 namespace singleton{ 17 getInstance = function(){ 18 if(self.instance == null){ 19 self.instance = ..singleton(); 20 } 21 return self.instance; 22 } 23 } 24 25 var s1 = singleton.getInstance(); 26 27 var s2 = singleton.getInstance(); 28 29 30 if(s1 == s2){ 31 console.log("两个对象是相同的实例"); 32 console.log(s1.test(),type(s1)) 33 console.log(s2.test(),type(s2)) 34 }else { 35 console.log("两个对象不是是相同的实例"); 36 } 37 38 39 console.pause(true);
标签:singleton,console,log,s2,aardio,模式,instance,单例,s1 From: https://www.cnblogs.com/axuanup/p/18214134