Randomblue
Randomblue

Reputation: 116273

How do symbols work?

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?

Upvotes: 5

Views: 355

Answers (1)

Esailija
Esailija

Reputation: 140220

You should try without new:

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

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

Upvotes: 2

Related Questions