Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
177
rated 0 times [  180] [ 3]  / answers: 1 / hits: 70914  / 11 Years ago, wed, february 12, 2014, 12:00:00

UPDATE: Recently a brilliant article from Mozilla came up. Read it if you're curious.



As you may know they are planning to include new Symbol primitive type in ECMAScript 6 (not to mention some other crazy stuff). I always thought that the :symbol notion in Ruby is needless; we could easily use plain strings instead, like we do in JavaScript. And now they decide to complicate things in JS with that.


I don't understand the motivation. Could someone explain to me whether we really need symbols in JavaScript?


More From » symbols

 Answers
35

The original motivation for introducing symbols to Javascript was to enable private properties.



Unfortunately, they ended up being severely downgraded. They are no longer private, since you can find them via reflection, for example, using Object.getOwnPropertySymbols or proxies.



They are now known as unique symbols and their only intended use is to avoid name clashes between properties. For example, ECMAScript itself can now introduce extension hooks via certain methods that you can put on objects (e.g. to define their iteration protocol) without risking them to clash with user names.



Whether that is strong enough a motivation to add symbols to the language is debatable.


[#72568] Tuesday, February 11, 2014, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
tatyanna

Total Points: 552
Total Questions: 96
Total Answers: 96

Location: Cook Islands
Member since Thu, May 21, 2020
4 Years ago
;