Map和WeakMap之间的主要区别(如我所想):
如果我们在Map中存储了一个对象,然后在其他地方没有引用该对象,那么该对象仍然不会被包含在垃圾收集过程中,我们仍然可以在 Map 中访问它。
但是,如果它存储在WeakMap中,然后该对象没有被引用到代码中的其他地方,那么它将被垃圾收集。
现在看看这个例子,我得到的输出看起来就像是 Map 中不再有引用的对象现在正在被垃圾收集:
const userRoles = new Map();
let Corey = { name: "Corey", age: "40" };
userRoles.set(Corey, "Admin");
Corey = null;
console.log(userRoles.get(Corey)); // Undefined ??
规范是否发生了变化,因为我在 MDN 中看不到某种声明,或者我误解了什么?