It is my understanding that symbols were going to be a way to have private methods. This aspect of them - the reason they were going to be introduced in the first place - was dropped, and so you are left with its current limited form with a much narrower use-case.<p>From A Rossberg (he's also the guy behind SoundScript) in a March 2014 stackoverflow post (<a href="http://stackoverflow.com/questions/21724326/why-bring-symbols-to-javascript/22280202#22280202" rel="nofollow">http://stackoverflow.com/questions/21724326/why-bring-symbol...</a>):<p>"Enabling private properties ... was indeed the original motivation for introducing symbols into JavaScript. Unfortunately, however, they ended up being severely downgraded, and not private after all.<p>They are now known as unique symbols, and their only use is to avoid name clashes between properties....Whether that is strong enough a motivation to add symbols to the language is debatable."<p>and from R. Waldron as part of this response (<a href="https://esdiscuss.org/topic/proposal-about-private-symbol" rel="nofollow">https://esdiscuss.org/topic/proposal-about-private-symbol</a>) to a proposal about a private symbol (Dec 2014)<p>"Ultimately it was decided that Symbol is just a symbol and that "private" things will be dealt with orthogonally (and at a later date)."<p>YMMV
> Calling Symbol() creates a new symbol, a value that’s not equal to any other value.<p>Lisp:<p><pre><code> CL-USER 1 > (eq (make-symbol "Foo") (make-symbol "Foo"))
NIL
</code></pre>
> Symbols aren’t exactly like anything else<p><pre><code> CL-USER 2 > (type-of (make-symbol "Foo"))
SYMBOL
</code></pre>
> Trying to concatenate a symbol with strings will result in a TypeError.<p><pre><code> CL-USER 3 > (concatenate 'string "abc" 'foo "def")
Error: In a call to LENGTH: FOO (of type SYMBOL) is not of type SEQUENCE.
</code></pre>
> There are three ways to obtain a symbol.<p>> Call Symbol()<p><pre><code> (make-symbol "FOO")
</code></pre>
> Call Symbol.for(string)<p><pre><code> (find-symbol "FOO")
</code></pre>
Other than that symbols in Common Lisp have a package, a value, a function and a property list. Symbols can be interned in a package or not. So-called Keyword symbols are in the package KEYWORD and have itself as the value. :I-AM-A-KEYWORD evaluates to :I-AM-A-KEYWORD.
As a very green JS programmer, I found this post informative and fun.<p>I think this sentence has an editing mistake in it:<p><pre><code> It’s simply a property whose name is not a symbol rather than a string.
</code></pre>
I think that the "not" shouldn't be there, the double negation makes the sentence fail to parse for me at least (ObCaveat: not a native speaker).
I wrote a lil' article about using Symbols a month ago - <a href="https://medium.com/code-ops/party-tricks-with-es6-symbols-ee328fdb6c4b" rel="nofollow">https://medium.com/code-ops/party-tricks-with-es6-symbols-ee...</a><p>Probably the most interesting use I've found for Symbols so far is to detect if an object / function was created by a specific factory - <a href="https://gist.github.com/yoshuawuyts/2bf8d5394e6f995791a0" rel="nofollow">https://gist.github.com/yoshuawuyts/2bf8d5394e6f995791a0</a>
Symbols are odd. Maybe it's just me but they feel like they work "funny" in JavaScript.<p>As the article says you can't implicitly convert a symbol's description to string. Symbol is now the only native object in JavaScript that has this behavior.<p>var str = "something" + "str"; // Works<p>var num = "something" + 5; // Works<p>var func = "something" + function () { }; // Works<p>var obj = "something" + { some: "test" }; // Works<p>var bool = "something" + true; // Works<p>var dt = "something" + Date.now(); // Works<p>var und = "something" + undefined; // Works<p>var nul = "something" + null; // Works<p>var nan = "something" + NaN; // Works<p>var sym = "something" + Symbol("test"); // Throws TypeError<p>Another thing, which is more of a style thing in my opinion, is you can actually define a property with a Symbol which just seems awkward to me. I mean sure you can use it as a property by design so why wouldn't you be able to use defineProperty? I always felt those should be public types of properties where you can add additional logic where necessary.<p>var obj = { };<p>Object.defineProperty(obj, Symbol("MyProp"), {<p><pre><code> get: function () { return 15; }
</code></pre>
});<p>I feel like almost the same thing with Symbol could be accomplished with a simple UUID generator. It's a neat little thing it just feels awkward to me with how JavaScript works.
I don't understand why the author compares ES6 symbols to Ruby symbols; they're <i>nothing</i> 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 <i>different</i> object, even if they have the same name (description)
Looks like Symbols is a type being introduced because objects, when used as a key in another object, gets turned into a string, unlike Lua tables can be used as keys of other tables, and are guaranteed to not be equal to any other value. Lua tables are built-in symbols. Javascript's objects are not.
I wish javascript had some kind of flag so you could get it to act sanely. All the comparison operators would work like they do in sane languages; type conversion wouldn't be <i>quite</i> so automatic and insane, etc.
This is very limited symbol type. They might have named them keywords instead. It's just interned string with a new type.<p>(It's like Common Lisp symbols limited inside the keyword package)
I'm not entirely convinced with the function-call syntax for declaring a Symbol.<p>Why not using something more of the lines with Ruby's
:symbol
syntax?
yet another duct tape kind of feature, from people already too corrupted by js, that will be further abused and cause yet more duct tape features in ES7.<p>this is nothing but a more convoluted way of the pattern that sets a unique object as a unique value for comparison. well, it adds a label for easy debug. hooray.
<i>Call Symbol.for(string). This accesses a set of existing symbols called the symbol registry. Unlike the unique symbols defined by Symbol(), symbols in the symbol registry are shared. If you call Symbol.for("cat") thirty times, it will return the same symbol each time. The registry is useful when multiple web pages, or multiple modules within the same web page, need to share a symbol.</i><p>Have they added inbuilt support for globals here? People could easily add their own global symbols, why does this need to be built in?
I'm not from a Ruby/Lisp background, so I don't know how symbols are typically used, but the explanation of why symbols are required is to prevent collisions if two libraries chose to use the same property name; so given that, what is the point of the Symbol registry? I don't understand what the value of a symbol that is referenced by a string is, over simply using the string?
If you think that this language feature has no equivalent in your favorite language, it is similar to Ruby's :symbol, where it is mostly used as keys to their hash tables (they're implemented with hashes internally).<p>Common Lisp has something even closer for its macro system. When you write code that writes code, you often need to create new variable names that won't collide or shadow anything. `(gensym)` does that: <a href="http://www.lispworks.com/documentation/lw50/CLHS/Body/f_gensym.htm#gensym" rel="nofollow">http://www.lispworks.com/documentation/lw50/CLHS/Body/f_gens...</a>.<p>Here's a list of languages with support for symbols. You will see even Objective-C has it: <a href="https://en.wikipedia.org/wiki/Symbol_%28programming%29#Support" rel="nofollow">https://en.wikipedia.org/wiki/Symbol_%28programming%29#Suppo...</a>.<p>I'd be curious to learn how it is implemented in JS engines.
> Sometimes it would be awfully convenient to stash some extra data on a JavaScript object that really belongs to someone else.<p>Convenient yes, a good idea, no.<p>> Other code using for-in or Object.keys() may stumble over the property you created.<p>> The standard committee may decide to add an .isMoving() method to all elements. Then you’re really hosed!<p>So I dunno, maybe don't stash properties into an object that doesn't belong to you? It's this sort of thing that makes me hate the culture around JavaScript. Hacks upon hacks upon hacks just to save a little effort.