Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 240
Randomblue Avatar asked Nov 02 '22 18:11

Randomblue


1 Answers

You should try without new:

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

var a = {};
a[mySymbol] = 'Hello!';
like image 50
Esailija Avatar answered Nov 09 '22 10:11

Esailija