首页 新闻 会员 周边

关于JS的内存泄漏跟JS代码的规范问题大家是怎么处理的?

3
悬赏园豆:5 [已解决问题] 解决于 2012-03-19 08:03

关于JS的内存泄漏跟JS代码的规范问题大家是怎么处理的?

比如这篇文章(JavaScript 中的内存泄露模式)中提到为防止内存泄露,把代码写成这样:

方法一:

    <html>
<body>
<script type="text/javascript">
document.write(
"Avoiding a memory leak by adding another closure");
window.onload
=function outerFunction(){
var anotherObj =function innerFunction()
{
// Some logic here
alert("Hi! I have avoided the leak");
};
(
function anotherInnerFunction(){
var obj = document.getElementById("element");
obj.onclick
=anotherObj })();
};
</script>
<button id="element">"Click Here"</button>
</body>
</html>

方法二:

    <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 here
alert("Hi! I have avoided the leak");
}

</script>
</head>
<body>
<button id="element">"Click Here"</button>
</body>
</html>

方法一的代码明显比较乱,方法二很多人的写法都是写成匿名方法(方法表达式)的形式。

请问在JS的内存泄漏跟JS代码的规范间大家是怎么平衡的呢?

观海云不远的主页 观海云不远 | 初学一级 | 园豆:89
提问于:2012-03-14 09:10
< >
分享
最佳答案
0

http://www.cnblogs.com/artwl/archive/2012/03/13/2394060.html
这一片就是讲js内存泄露的问题的。你看下吧。

收获园豆:5
rains | 小虾三级 |园豆:860 | 2012-03-14 13:46
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册