Last active
January 31, 2020 17:35
-
-
Save r3dm1ke/3093abd95b2e7f95caf4238a254c0521 to your computer and use it in GitHub Desktop.
A simplified version of Symbol.for() function
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
| 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