javascipt singleton

function App(){

    		this. name = arguments[0];

	}
	App.prototype.getName = function(){
	    return this.name;
	}
	var InstanceFactory =(function(){
	    var instance = null;
	    return function(){

	        if(!instance){
	            instance = new App(arguments[0]);
	         }
	         return instance;
    	      }
    
	})()

	var instance = InstanceFactory('testApp');
	var instance1 = InstanceFactory('testApp1');
	console.log(instance == instance1 )//ture

利用閉包延長單例對象的生命週期實現js的單利javascript

相關文章
相關標籤/搜索