Skip to content

Instantly share code, notes, and snippets.

@snichme
Created June 29, 2011 22:01
Show Gist options
  • Save snichme/1055123 to your computer and use it in GitHub Desktop.
Save snichme/1055123 to your computer and use it in GitHub Desktop.
Singleton pattern in Javascript
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