by jansc on 6/12/15, 9:11 AM with 69 comments
by nni on 6/12/15, 2:25 PM
From A Rossberg (he's also the guy behind SoundScript) in a March 2014 stackoverflow post (http://stackoverflow.com/questions/21724326/why-bring-symbol...):
"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.
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."
and from R. Waldron as part of this response (https://esdiscuss.org/topic/proposal-about-private-symbol) to a proposal about a private symbol (Dec 2014)
"Ultimately it was decided that Symbol is just a symbol and that "private" things will be dealt with orthogonally (and at a later date)."
YMMV
by lispm on 6/12/15, 1:43 PM
Lisp:
CL-USER 1 > (eq (make-symbol "Foo") (make-symbol "Foo"))
NIL
> Symbols aren’t exactly like anything else CL-USER 2 > (type-of (make-symbol "Foo"))
SYMBOL
> Trying to concatenate a symbol with strings will result in a TypeError. CL-USER 3 > (concatenate 'string "abc" 'foo "def")
Error: In a call to LENGTH: FOO (of type SYMBOL) is not of type SEQUENCE.
> There are three ways to obtain a symbol.> Call Symbol()
(make-symbol "FOO")
> Call Symbol.for(string) (find-symbol "FOO")
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.by unwind on 6/12/15, 10:42 AM
I think this sentence has an editing mistake in it:
It’s simply a property whose name is not a symbol rather than a string.
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).by yoshuaw on 6/12/15, 10:48 AM
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 - https://gist.github.com/yoshuawuyts/2bf8d5394e6f995791a0
by BinaryIdiot on 6/12/15, 5:01 PM
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.
var str = "something" + "str"; // Works
var num = "something" + 5; // Works
var func = "something" + function () { }; // Works
var obj = "something" + { some: "test" }; // Works
var bool = "something" + true; // Works
var dt = "something" + Date.now(); // Works
var und = "something" + undefined; // Works
var nul = "something" + null; // Works
var nan = "something" + NaN; // Works
var sym = "something" + Symbol("test"); // Throws TypeError
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.
var obj = { };
Object.defineProperty(obj, Symbol("MyProp"), {
get: function () { return 15; }
});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.
by skrebbel on 6/12/15, 10:09 AM
by meric on 6/12/15, 11:42 AM
by IshKebab on 6/12/15, 12:17 PM
by nabla9 on 6/12/15, 10:40 AM
(It's like Common Lisp symbols limited inside the keyword package)
by welfare on 6/12/15, 10:51 AM
Why not using something more of the lines with Ruby's :symbol syntax?
by asdf99 on 6/12/15, 9:59 AM
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.
by agd on 6/12/15, 12:47 PM
Have they added inbuilt support for globals here? People could easily add their own global symbols, why does this need to be built in?
by jordanwallwork on 6/12/15, 12:23 PM
by espadrine on 6/12/15, 10:20 AM
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: http://www.lispworks.com/documentation/lw50/CLHS/Body/f_gens....
Here's a list of languages with support for symbols. You will see even Objective-C has it: https://en.wikipedia.org/wiki/Symbol_%28programming%29#Suppo....
I'd be curious to learn how it is implemented in JS engines.
by xienze on 6/12/15, 11:56 AM
Convenient yes, a good idea, no.
> Other code using for-in or Object.keys() may stumble over the property you created.
> The standard committee may decide to add an .isMoving() method to all elements. Then you’re really hosed!
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.