Skip to content

Instantly share code, notes, and snippets.

@Janking
Created May 26, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Janking/5c9997fbf334e1b09103 to your computer and use it in GitHub Desktop.
Save Janking/5c9997fbf334e1b09103 to your computer and use it in GitHub Desktop.
传统单例模式
//传统单例模式
var Singleton = function(name){
this.name = name;
this.instance = null;
}
Single.prototype.getName = function(){
console.log(this.name);
};
Singleton.getInstance = function(name){
if(!this.instance){
this.instance = new Singleton(name);
}
return this.instance;
};
var a = Singleton.getInstance('sven1');
var b = Singleton.getInstance('sven2');
console.log(a === b) // true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment