Why are WeakMaps not interable?
Javascript现在具有称为
为什么不能迭代这些实例,即使用
注意:这不是重复的问题。 另一个问"如何",这问"为什么"。
MDN对此进行了解释
If they were, the list would depend on the state of garbage collection, introducing non-determinism.
但是您可能想知道这到底意味着什么。
首先,垃圾回收是不确定的。 JavaScript程序可以由垃圾回收器根据运行环境的全权酌情决定是否被垃圾回收。
现在假设我们有以下程序:
1 2 3 4 5 6 7 8 9 | const weakMap = new WeakMap(); const key1 = { data: 123 }; weakMap.set(key1,"value"); weakMap.set({ data: 456 },"value"); for (const [key, value] of weakMap) { // This does not actually work, of course console.log(`${key} is ${value}`); } |
您希望该程序产生什么?
我们无法确定,因为实际上
该程序将表现出不可预测的行为,因此是不允许的。
根据文档:
One difference to Map objects is that WeakMap keys are not enumerable (i.e., there is no method giving you a list of the keys). If they were, the list would depend on the state of garbage collection, introducing non-determinism.
同样,