Re:ES6 in Depth – Symbols
by espadrine on Friday June 12, 2015 @06:23AM
(#9704977)
The most common use-case for them is to generate symbols that don't collide, though.
[ Parent
| on HN ]
`Symbol.for` uses a global registry and the whole thing is "stringly typed".
Looks like an anti-pattern to me.
If you want one particular symbol from some library, you could import it.
Re:ES6 in Depth – Symbols
by artursapek on Friday June 12, 2015 @11:33AM
(#9706498)
This was the one thing I didn't understand. Why is using a global Symbol registry any better than just using string properties like usual?
Re:ES6 in Depth – Symbols
by Ded7xSEoPKYNsDd on Friday June 12, 2015 @12:02PM
(#9706717)
There won't be a problem if the string happens to be e.g. "toString", I guess. Although they added a 'Map' for that, too.
s1 = Symbol("cat");
s2 = Symbol.for("cat");
s3 = Symbol.for("cat");
s1 == s2; // false
s2 == s3; // true
You can still have Symbols outside the registry.
Re:ES6 in Depth – Symbols
by artursapek on Saturday June 13, 2015 @09:24PM
(#9713460)
That I understand. What I don't understand is why s2 and s3 shouldn't just be vanilla strings.