javascript闭包产生的内存泄漏

来源:互联网 发布:淘宝设置分享有礼 编辑:程序博客网 时间:2024/06/06 01:26

使用时,最好不要使用闭包,可以将函数声明后,直接调用函数名即可。

<html><head><script type="text/javascript">document.write("Avoid leaks by avoiding closures!");window.onload=function(){var obj = document.getElementById("element");obj.onclick = doesNotLeak;}function doesNotLeak(){//Your Logic herealert("Hi! I have avoided the leak");}</script></head><body><button id="element">"Click Here"</button></body></html>