2

From what I've read ES6 symbols "only use is to avoid name clashes between properties"..

If this is the case and I wanted to check for name collisions on an object, why not just use a simple function to check, and then use a different property name if the check returns true? Why the introduction to an entire new data type ? (I'm sure I'm naive in asking this, but I don't understand)

Example:

var obj = {
    prop1: "some prop",
    prop2: "some prop",
    prop3: "some prop"
}

function propChecker(propName) {
    if (propName in obj) {
        console.log("Pick a different property");
    }else{
        console.log("Property name is available");
    }
}

In this link the author of the accepted answer says "EcmaScript itself can now introduce extension hooks via certain methods you can put on objects (e.g. to define their iteration protocol) without running the risk of clashing with user names."

I am curious what this means in more simple-speak.

I'm guessing the "real" use of symbols is that of a tool to help those connected to the standards body implement new features without breaking preexisting code.

  [1]: https://stackoverflow.com/questions/21724326/why-bring-symbols-to-javascript
Community
  • 1
  • 1
William
  • 4,422
  • 17
  • 55
  • 108

1 Answers1

2

Why not just use a simple function to check, and then use a different property name if the check returns true?

Because that would mean everyone needed to do these checks. And there needed to be a protocol on how to use a different property name.

Also, one needs to assume that existing code already uses all possible property names. If you want to introduce a new, standard method name to use in an iteration protocol, which one could you choose? There is no name that you can pick where it is assured that it doesn't collide with anything.

I'm guessing the "real" use of symbols is that of a tool to help those connected to the standards body implement new features without breaking preexisting code.

No, you don't have to be connected to the standards body to use symbols. Everyone can introduce his own property names that don't clash with other code. They're useful for all kinds of libraries that need to work with arbitrary objects and don't want to break encapsulation.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • In this article: https://www.sitepen.com/blog/2016/04/13/es6-symbols-drumroll-please/ The author says " [Symbols] were added to the language in order to solve the problem of extending the functionality of Object while maintaining backwards-compatibility with code written in earlier versions of JavaScript." Is this a correct statement ? – William Mar 23 '17 at 02:30
  • Yes, but that's not an exclusive reason. – Bergi Mar 23 '17 at 02:40
  • I'm trying to get a concise bullet-point list as to why these things exist. What are all the primary reasons ? – William Mar 23 '17 at 02:50
  • Why these things exist? The bottom line, because they were deemed useful by the language committee. How that happened and for what reasons, you'll need to work through the es-discuss archives. – Bergi Mar 23 '17 at 03:04