0
WeakMap在JavaScript中的正确用法是什么?我使用它时可能会发生什么样的时间问题?特别是,我想知道在以下情况会发生什么:使用EcmaScript中的WeakMap时的时序问题注意事项
var wm1 = new WeakMap()
var o1 = {},
o2 = function(){},
o3 = window;
// in other method:
wm1.set(o1, 37);
wm1.set(o2, "azerty");
if (wm1.has(o2)) {
//Garbage collection happen here, objects from wm1 may no longer exists
Console.log(wm1.get(o2)) // what will happen here? just undefined? null?
}
GC如何影响WeakMaps?
更新:我不好,我错过了事实,你不能有字符串作为WeakMap键,如果我考虑到,其实我的问题不作。
谢谢!如果map key是一个字符串呢? – vmg
@VitaliyGanzha:这就是为什么你不能在弱集合中使用字符串作为键的原因:-) – Bergi