相关疑难解决方法(0)

JavaScript WeakMap 不断引用 gc 对象

我正在使用 JavaScript 弱图,在 google chrome 开发者控制台中尝试此代码后,使用 --js-flags="--expose-gc" 运行,我不明白为什么弱图一直引用 ab 如果 a 是gc'ed。

var a = {listener: function(){ console.log('A') }}
a.b = {listener: function(){ console.log('B') }}

var map = new WeakMap()

map.set(a.b, [])
map.set(a, [a.b.listener])

console.log(map) // has both a and a.b

gc()
console.log(map) // still have both a and a.b

a = undefined
gc()
console.log(map) // only have a.b: why does still have a reference to a.b? Should'nt be erased?
Run Code Online (Sandbox Code Playgroud)

javascript garbage-collection weakmap ecmascript-6

7
推荐指数
1
解决办法
970
查看次数