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);
しかし、結果として空のobject
fromを取得します。babel-node
{}
object
どうしたの?
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);
しかし、結果として空のobject
fromを取得します。babel-node
{}
object
どうしたの?
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 Map
s 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.
問題が見つかりました。iojs
の代わりに使用するとnode
、うまく機能します。
babel-node test/t.js
結果は次のようになります。
object
Map { 'a' => 'b', 'b' => 1, 'c' => { a: 12 } }
注: iojs
インストーラーは のシンボリック リンクを に変更node
しiojs
ます。