This question already has an answer here:
JavaScript(ES6) WeakMap garbage collection when set an object to null
                                
                                    (1个答案)
                                
                        
                                去年关闭。
            
                    
删除对象时,weakmap保持对它的引用。
但是正常的行为是:oyu删除对象时,它将自动从weakmap中删除,并且weakmap不会导致内存泄漏。
weakmapdelete有什么问题吗?

let a =  { aa : { aa : 123 } };
const w = new WeakMap();
w.set(a.aa,"hello");
delete a.aa
console.log(w);// shows that '{aa:123}' is still there in weakmap


我已经关闭并打开devtool,而{aa:123}仍然存在。
期望weakmap为空

最佳答案

您的delete()函数应如下所示:

 w.delete(a.aa);

关于javascript - javascriptweakmap保留对已删除对象的引用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54017566/

10-09 07:08