Afshin Mehrabani
Afshin Mehrabani

Reputation: 34939

`Map` returns empty object with BabelJS

I'm trying to run following code with BabelJs:

var m = new Map();
m.set('a', 'b');
m.set('b', 1);
m.set('c', { a: 12 });

console.log(m);
console.log(typeof m);

But I get an empty object from babel-node as a result:

{}
object

What's the problem?

Upvotes: 0

Views: 376

Answers (2)

Felix Kling
Felix Kling

Reputation: 816482

What's the problem?

There is no problem. Maps simply don't have own enumerable properties. If your question is why you are seeing {} instead of Map {....}, that's because your environment doesn't have support for Maps yet, hence core-js (which is what Babel uses) polyfills them.

It is not possible (afaik) to override how console.log should display a value, hence you are only seeing an empty object. The console just shows you some representation of the value, according whatever the browser vendor deemed useful.


To make my point clearer, lets have a look what you get for console.log(document.body):

> console.log(document.body)
<body class=​"...">​…​</body>​

Does this mean document.body is a string containing HTML? Of course not. document.body is a DOM element. The console just renders its HTML representation because someone thought this would be more helpful than just dumping all the properties of a DOM element.

If you really want to see all the properties of an object, console.dir takes you at least one step closer to that.

Upvotes: 2

Afshin Mehrabani
Afshin Mehrabani

Reputation: 34939

Ok I found the problem. If you use iojs instead of node, it works well:

babel-node test/t.js

And the result would be:

object
Map { 'a' => 'b', 'b' => 1, 'c' => { a: 12 } }

Note: iojs installer changes the symlink of node to iojs.

Upvotes: 0

Related Questions