var mySingleton=(function(){ //实例保持了Singleton的一个引用 var instance; function init(){ //Singleton //私有方法和变量 function privateMethod(){ console.log("I am private"); } var privateVariable="I am also private"; var privateRandomNumber=Math.random(); return{ //公有方法和变量 publicMethod:function(){ console.log("The public can see me!"); }, publicProperty:"I am also public", getRandomNumber:function(){ return privateRandomNumber; } }; } return{ //获取singleton的实例,如果存在则返回,不存在就创建新实例 getInstance:function(){ if(!instance){ instance=init(); } return instance; } } })();
var myBadSingleton=(function(){ //实例保存了singleton的一个引用 var instance; function init(){ //Singleton var privateRandomNumber=Math.random(); return{ getRandomNumber:function(){ return privateRandomNumber; } }; } return{ //每次都创建新实例 getInstance:function(){ instance=init(); return instance; } }; })();
var singleA=mySingleton.getInstance(); var singleB=mySingleton.getInstance(); console.log(singleA.getRandomNumber()===SingleB.getRandomNumber()); //true var badSingleA=myBadSingleton.getInstance(); var badSingleB=myBadSingleton.getInstance(); console.log(badSingleA.getRandomNumber()!==badSingleB.getRandomNumber()); //true