Created
May 26, 2015 14:03
-
-
Save Janking/5c9997fbf334e1b09103 to your computer and use it in GitHub Desktop.
传统单例模式
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//传统单例模式 | |
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