Created
October 17, 2016 20:47
-
-
Save oosby/e181775b80b42f887ea48c1309dc4568 to your computer and use it in GitHub Desktop.
Singleton Maker
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
function singletonMaker(key, klassname, namespace) { | |
if (!window[namespace]) { | |
window[namespace] = {}; | |
} | |
const keySymbol = Symbol.for(key); | |
const globalSymbols = Object.getOwnPropertySymbols(namespace); | |
const hasKey = (globalSymbols.indexOf(keySymbol) > -1); | |
if (!hasKey) { | |
window[namespace][key] = new klassname(); | |
Object.seal(window[namespace][key]); | |
return window[namespace][key]; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment