Question

Node.JS v0.11.3 claims to have support for ECMAScript 6 symbols with the --harmony_symbols flag (see here). The latest draft says

Properties are identified using key values. A key value is either an ECMAScript String value or a Symbol value.

I have tried the following example

var mySymbol = new Symbol('Test symbol');
console.log(mySymbol.name); // prints 'Test symbol', as expected

var a = {};
a[mySymbol] = 'Hello!';

but I get an error on the last line

TypeError: Conversion from symbol to string

How do symbols work? Is my example wrong, or does Node.JS actually not support symbols?

Était-ce utile?

La solution

You should try without new:

var mySymbol = Symbol('Test symbol');
console.log(mySymbol.name); // prints 'Test symbol', as expected

var a = {};
a[mySymbol] = 'Hello!';
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top