I'm experimenting with the new ES6 Symbols in Google Chrome, and after enabling "Experimental JavaScript" in Chrome Flags, I can use new Symbol()
in my code.
However, when I try to run the following code:
var sym = new Symbol();
var obj = {};
obj[sym] = 'Hello, symbols';
I get the following error:
TypeError: Conversion from symbol to string
What's wrong with my code?
Thanks!
Since symbols are values, not objects, they are created by a plain function call to
Symbol
, not by invokingnew
on it. In the version of Chrome you are using,new Symbol
gives you a wrapper object for such a value (i.e., you can get the actual symbol by invoking.valueOf
on the result).Since this is a common pitfall, the ES6 draft spec recently changed to disallow constructor invocation of
Symbol
altogether. That is, you already get an exception on the first line of your code. This change has already been implemented in V8, but hasn't made it into Chrome yet.