Created
June 29, 2011 22:01
-
-
Save snichme/1055123 to your computer and use it in GitHub Desktop.
Singleton pattern 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
var obj = (function(){ | |
function Class() { | |
} | |
Class.prototype = { | |
_var : null, | |
func1 : function() { | |
}, | |
func2 : function() { | |
} | |
}; | |
var _instance; | |
return { | |
instance : function(){ | |
if (_instance == null) { | |
_instance = new Class(); | |
_instance.constructor = null; | |
} | |
return _instance; | |
} | |
}; | |
})(); | |
// Usage | |
obj.instance().func1(); | |
// or | |
var o = obj.instance(); | |
o.func1(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment