Last active
April 23, 2019 19:49
-
-
Save aderbas/659502910b6f687f124f9738789242d6 to your computer and use it in GitHub Desktop.
Javascript "Singleton"
This file contains hidden or 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
/** | |
* 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