【发布时间】:2009-12-09 05:42:24
【问题描述】:
我有这段代码,我在为项目构建的一些 JavaScript 组件上使用了它。现在我想知道以下代码是否存在内存泄漏。
哪个选项最合适,A 还是 B,还是有更好的方法?
var component = function(){
var self = this; //A - not sure there's a leak here
this.foo = function(){
//var self = this; //B. I can do this but I want to use self in other method as well
var dom = getElementById('someid');
dom.onclick = function(){
self.foo2(); // here I used the self reference
//i cannot use this here, because it refer to dom
}
}
this.foo2 = function(){
var dom = getElementById('someid');
dom.onclick = function(){
self.foo2(); //here I used the self reference
//i cannot use this here, because it refer to dom
}
}
};
// some usage
var c1 = new component();
c1.foo();
【问题讨论】:
-
不知道——我对前世的内存泄漏检测是:写一个脚本,如果浏览器变得无响应,那就是内存泄漏。