In JavaScript, I can create Symbol -primitives like this:
const ex1 = Symbol('example1');
const ex2 = Symbol.for('example2');
And I can get the original string that was used to create the Symbol by accessing the Symbol.prototype.description, or by calling the Symbol.keyFor() -method like so:
ex1.description; // 'example1'
ex2.description; // 'example2'
// OR
Symbol.keyFor(ex1); // undefined
Symbol.keyFor(ex2); // 'example2'
My questions are;
- Is there any difference between the two methods, other than
Symbol.keyFornot working for Symbols that are not in the global registry (i.e. created fromSymbol.for())? It seems to me they are otherwise used to achieve the exact same thing. - Is there a reason to ever prefer using
Symbol.keyFor()instead ofSymbol.prototype.description? Since thedescriptionworks for all Symbols, whilekeyForworks only for ones in the global registry.