为什么console.log将 WeakSet 显示为<items unknown>?[13:37:11] [~] nodeWelcome 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上下文:我在 ES6中的WeakSet上遇到了以下示例。let student1 = { name: 'James', age: 26 };let student2 = { name: 'Julia', age: 27 };const roster = new WeakSet([student1, student2]);console.log(roster);该示例表明它应该打印WeakSet {Object {name: 'Julia', age: 27}, Object {name: 'Richard', age: 31}}但在节点 v14.4.0 中,它会打印WeakSet { <items unknown> }
1 回答
www说
TA贡献1775条经验 获得超8个赞
node-js 团队认为很难正确实现它。这是问题:https ://github.com/nodejs/node/issues/19001
这意味着 WeakSet 可以正常工作,但 console.log 将始终输出一个空的 WeakSet
如果您仍想检查 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 }));
添加回答
举报
0/150
提交
取消