Skip to content

Instantly share code, notes, and snippets.

@vanderlin
Last active August 29, 2015 13:56
Show Gist options
  • Select an option

  • Save vanderlin/9053365 to your computer and use it in GitHub Desktop.

Select an option

Save vanderlin/9053365 to your computer and use it in GitHub Desktop.
Simple Singleton JS
var MyClass = (function() {
var instance;
function init() {
// Private
var className = "CLASS";
var cb;
// public
return {
name:"todd",
sayName:function() {
if(cb) {
cb();
}
},
setCallback:function(callback) {
cb = callback;
}
};
};
// Singleton Instance
return {
getInstance: function() {
if(!instance) {
instance = init();
}
return instance;
}
};
})();
// this is just a ref to getInstance to use globaly
var globalObject = MyClass.getInstance();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment