7

--js-flags="--expose-gc" を指定して実行している Google Chrome 開発者コンソールでこのコードを試した後、JavaScript のウィークマップで経験しています。 GCした。

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?
4

1 に答える 1