Created
March 28, 2013 12:09
-
-
Save poksme/5262659 to your computer and use it in GitHub Desktop.
How to make a singleton in javascript
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
function MySingletonObject() { | |
// SINGLETON CONSTRUCTOR | |
if (MySingletonObject.prototype._singletonInstance) { | |
return MySingletonObject.prototype._singletonInstance; | |
} | |
MySingletonObject.prototype._singletonInstance = this; | |
// | |
// OBJECT PROPERTIES | |
this.PublicProperty = 42; | |
var PrivateProperty = "foo"; | |
// | |
// OBJECT METHODS | |
this.PublicMethod = function () { | |
return true; | |
} | |
var PrivateMethod = function () { | |
return "bar" | |
} | |
// | |
} | |
// USAGE | |
var mso = new MySingletonObject(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment