简体   繁体   English

我是否需要在窗口卸载时显式销毁JavaScript对象?

[英]Do I need to explicitly destroy JavaScript objects on window unload?

I have a JavaScript widget that is hosted on websites. 我有一个托管在网站上的JavaScript小部件。 This widget tracks state in a number of variables in its local namespace. 此小组件在其本地命名空间中跟踪许多变量中的状态。 Moreover, it attaches listeners for several events, such as mouse movement. 此外,它还为几个事件(如鼠标移动)附加了监听器。

Should I explicitly destroy both state-tracking variables and detach event listeners on window unload? 我是否应该明确销毁状态跟踪变量并在窗口卸载时分离事件侦听器? Or is it ok to rely on the browser to do a good job of cleaning up after the user leaves the page that hosts my widget? 或者,在用户离开托管我的小部件的页面后,依靠浏览器做好清理工作是否可以?

Once you get rid of the window you get rid of everything in it. 一旦你摆脱了窗户,你就摆脱了它的一切。 Hence, no need to do your own garbage collection. 因此,无需进行自己的垃圾回收。 If a certain browser has a memory leak, that is not your fault and your de facto garbage collection probably wouldn't help anyway. 如果某个浏览器有内存泄漏,那不是你的错,你的事实上的垃圾收集可能无论如何也无济于事。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM