Search code examples
node.jsecmascript-harmonyecmascript-6

Map collection in Nodejs Harmony/ES6 broken?


seems that Map in Nodejs 0.10.20 is broken. Im starting it up with the --harmony (which includes the --harmony_collections flag).
For reference check out http://dailyjs.com/2012/10/15/preparing-for-esnext/.
The examples bellow run in Firefox 20.0.

in the Nodejs console I do the following

> var map = new Map([ ["name", "Nicholas"], ["title", "Author"]]);
undefined
> map
{}

so the contsructor initialization doesnt seem to work. then I follow the Firefox examples and they mostly dont work:

> console.log(map.has("name")); // true
false
undefined
> console.log(map.get("name")); // "Nicholas"
undefined
undefined
> console.log(map.has("title")); // true
false
undefined
> console.log(map.get("title")); // "Author"
undefined
undefined
> console.log(map.size()); // 2
TypeError: Object # has no method 'size'
at repl:1:17
at REPLServer.self.eval (repl.js:110:21)
at repl.js:249:20
at REPLServer.self.eval (repl.js:122:7)
at Interface. (repl.js:239:12)
at Interface.EventEmitter.emit (events.js:95:17)
at Interface._onLine (readline.js:202:10)
at Interface._line (readline.js:531:8)
at Interface._ttyWrite (readline.js:760:14)
at ReadStream.onkeypress (readline.js:99:10)

Im confused that these basic Map methods dont work. I have Nodejs version 0.10.20


Solution

  • It is working. I tested it on node v0.10.13

    var map = new Map();
    map.set("name", "Nicholas");
    map.set("title", "Author");
    
    > map.has("name");
    true
    > map.get("name");
    'Nicholas'
    > map.has("title");
    true
    > map.get("title");
    'Author'
    

    What you are trying is specific to Mozilla. It is not implemented in V8. Amongst what you tried size and array intialization with constructor is not there in V8. I was not able to find good documentation for es-harmony implementation in V8, which makes it hard to work with. Also it is not a standard yet, so implementations will vary.