简体   繁体   English

ES6:WeakSet 上的 console.log 给出<item unknown></item>

[英]ES6: console.log on WeakSet gives <item unknown>

Why does console.log display WeakSet as <items unknown> ?为什么console.log将 WeakSet 显示为<items unknown>

[13:37:11] [~] node
Welcome to Node.js v14.4.0.
Type ".help" for more information.
> let student1 = { name: 'James', age: 26 };
undefined
> let student2 = { name: 'Julia', age: 27 };
undefined
> const roster = new WeakSet([student1, student2]);
undefined
> console.log(roster);
WeakSet { <items unknown> }
undefined

Context: I came across below example on WeakSet in ES6.上下文:我在 ES6 中的WeakSet上遇到了以下示例。

let student1 = { name: 'James', age: 26 };
let student2 = { name: 'Julia', age: 27 };
const roster = new WeakSet([student1, student2]);
console.log(roster);

The example suggests it should print该示例表明它应该打印

WeakSet {Object {name: 'Julia', age: 27}, Object {name: 'Richard', age: 31}}

But in node v14.4.0 it prints但在节点 v14.4.0 中,它会打印

WeakSet { <items unknown> }

The node-js team decided that its hard to correctly implement that. node-js 团队认为很难正确实现它。 Here is the issue: https://github.com/nodejs/node/issues/19001这是问题: https://github.com/nodejs/node/issues/19001

So that mean that WeakSet work correctly BUT console.log will always output an empty WeakSet所以这意味着 WeakSet 可以正常工作但 console.log 将始终 output 为空的 WeakSet

If you still wanna inspect the WeakMap you can do that by using utils inspect:如果您仍想检查 WeakMap,您可以使用 utils inspect 来做到这一点:

const { inspect } = require('util');
let student1 = { name: 'James', age: 26 };
let student2 = { name: 'Julia', age: 27 };
const weakSet = new WeakSet([student1, student2]);
console.log(inspect(weakSet, { showHidden: true }));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM