Skip to content

Instantly share code, notes, and snippets.

@r3dm1ke
Last active January 31, 2020 17:35
Show Gist options
  • Select an option

  • Save r3dm1ke/3093abd95b2e7f95caf4238a254c0521 to your computer and use it in GitHub Desktop.

Select an option

Save r3dm1ke/3093abd95b2e7f95caf4238a254c0521 to your computer and use it in GitHub Desktop.
A simplified version of Symbol.for() function
Symbol.prototype.for = (key) => {
// Safely getting the global registry
const symbolRegistry = window.symbolRegistry || {};
// Checking if this symbol exists
if (symbolRegisty.hasOwnProperty(key)) {
return symbolRegistry[key];
}
// It does not, creating a new one
const newSymbol = Symbol();
symbolRegistry[key] = newSymbol;
window.symbolRegistry = symbolRegistry;
return newSymbol;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment