singleton pattern
Make sure a class has only one instance , instantiates itself and provides this instance to the whole system.
Use a variable to mark whether an object has been created for a certain class. If so, the object created before will be returned directly when the next instance of the class is obtained.
var Singleton = function( name ){
this.name = name;
this.instance = null;
};
Singleton.prototype.getName = function(){
alert ( this.name );
};
Singleton.getInstance = function( name ){
if ( !this.instance ){
this.instance = new Singleton( name );
}
return this.instance;
};
var a = Singleton.getInstance( 'svengkhjb1' );
var b = Singleton.getInstance( 'sven2' );
var c=Singleton.getInstance('sdj');
console.log(a); //svengkhjb1
console.log(b);//svengkhjb1
console.log(c);//svengkhjb1