0

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?

3
  • 1
    this means that map implementation doesn't have any enumerable own properties, nothing more Commented May 14, 2015 at 11:15
  • @vkurchatkin but it has a different result in Google Chrome developer console Commented May 14, 2015 at 11:15
  • In Chrome Map is native and console support is. Try iojs 2.0.0, you will see the same result Commented May 14, 2015 at 11:24

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

1 Comment

So, what exactly is the problem? What does this answer mean? Do I have to use io.js instead of Node to make Babel work? Or to be able to use Maps?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.