4 ms·
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
by skrebbel 11y ago
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)
- espadrine 11y agos1 = 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.
- ahoge 11y ago`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.
- artursapek 11y agoThis was the one thing I didn't understand. Why is using a global Symbol registry any better than just using string properties like usual?
- Ded7xSEoPKYNsDd 11y agoThere won't be a problem if the string happens to be e.g. "toString", I guess. Although they added a 'Map' for that, too.
- towelguy 11y agos1 = Symbol("cat"); s2 = Symbol.for("cat"); s3 = Symbol.for("cat"); s1 == s2; // false s2 == s3; // true You can still have Symbols outside the registry.
- artursapek 11y agoThat I understand. What I don't understand is why s2 and s3 shouldn't just be vanilla strings.