如何避免Javascript事件绑定出现内存泄漏
"These memory leaks often
occur as a result of circular references between JavaScript objects and
objects within IE’s DOM (document object model)."
GPDE Team Blog
明显的DOM对象与 JavaScript对象循环引用很好判断,难的是隐含的循环引用判断!
隐含的循环引用需要通过作用域链进行分析判断!
考虑以下代码(感谢可爱的winter ^_^):
var e=document.getElementBy??("XX");
e.onclick=function(){}
scopechain分析:
scopechain.e = document.getElementBy??("XX");
function f(){}
function f(){}.[[scope]] = scopechain
e.onclick = f
很清晰,对不对?
"Javascript绑定事件时,只要DOM的事件里访问不了DOM自己的那个变量就行了"
winter
"IE现在的Patch搞得有时候泄露有时候不泄露"
winter
<button id="testx">No Memory Leak Event Bind</button>
<button id="testy">No Memory Leak Event Bind</button>
<script type="text/javascript">
var fooA = function(){
var f = function(){alert(this+'\n'+fooA);}
!function(){
var d = document.getElementById('testx');
d.onclick = f;
}()
}
fooA();
var fooB = function(){
this.onclick = function(){alert(this+'\n'+fooB);}
}
fooB.call(document.getElementById('testy'));
</script>
相关文档:
1>zInherit:
它是一个组件,用来继承基类的所有属性和方法。跟以前说到的原型链模式非常类似,只不过比原型更安全,也无须考虑参数问题。下面看看zInherit的用法:
该组件中只有两个方法:inheritfrom() instanceof()
func ......
(一)对象冒充
function A(name){
this.name = name;
this.sayHello = function(){alert(this.name+” say Hello!”);};
}
function B(name,id){
this.temp = A;
this.temp(name); &nbs ......
第一种方法:使用insertRow添加行,使用insertCell添加单元格,再用innerHTML填充单元格。使用deleteRow删除行,代码如下:
Javascript代码:
function addRow()
{
var root = document.getElementById("tbody")
var&nb ......
一、声明字符串:
var normal_monkey = "I am a monkey!<br>";
document.writeln("Normal monkey " + normal_monkey);
var bold_monkey = normal_monkey.bold();
document.writeln("Bold monkey "&n ......
在Java中,基本类型之间的强制转换也不是这样的,比如,整数要转换成字符串,必须使用Integer.toString()静态方法或者String.valueOf()静态方法,把字符串转换为整数,必须使用Integer.valueOf()。
可见,不能把JavaScript中的类型转换看作为“强制类型转换”。
在JavaScript中,Double类型和Int类型都是看作为 ......