0

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);

しかし、結果として空のobjectfromを取得します。babel-node

{}
object

どうしたの?

4

2 に答える 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=​"...">​…​&lt;/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.

于 2015-05-14T15:33:16.453 に答える
0

問題が見つかりました。iojsの代わりに使用するとnode、うまく機能します。

babel-node test/t.js

結果は次のようになります。

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

注: iojsインストーラーは のシンボリック リンクを に変更nodeiojsます。

于 2015-05-14T11:25:45.553 に答える