Skip to content

Instantly share code, notes, and snippets.

@aderbas
Last active April 23, 2019 19:49
Show Gist options
  • Save aderbas/659502910b6f687f124f9738789242d6 to your computer and use it in GitHub Desktop.
Save aderbas/659502910b6f687f124f9738789242d6 to your computer and use it in GitHub Desktop.
Javascript "Singleton"
/**
* let c = new MyClass();
* c.foo();
* let d = new MyClass();
* d.foo();
*/
(() => {
let instance;
// global
window.MyClass = function(){
if(instance){
return instance;
}
/** "class" scope implementation */
let count = 1;
this.foo = function(){
$('body').append($('<div/>').text('Singleton Test - Count: '+count)); // jquery append
count++;
}
/** end scope */
/** instance */
instance = this;
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment