Skip to content

Instantly share code, notes, and snippets.

@oosby
Created October 17, 2016 20:47
Show Gist options
  • Save oosby/e181775b80b42f887ea48c1309dc4568 to your computer and use it in GitHub Desktop.
Save oosby/e181775b80b42f887ea48c1309dc4568 to your computer and use it in GitHub Desktop.
Singleton Maker
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