Re:ES6 in Depth – Symbols
by skrebbel on Friday June 12, 2015 @06:09AM
(#9704925)
I don't understand why the author compares ES6 symbols to Ruby symbols; they're nothing alike. It's kind of the opposite: Ruby symbols are guaranteed to be the same object if they have the same name, ES6 symbols are guaranteed to be a different object, even if they have the same name (description)
[ Parent
| on HN ]
s1 = Symbol.for('cat')
s2 = Symbol.for('cat')
s1 === s2 // true
The most common use-case for them is to generate symbols that don't collide, though.`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.