我有这个代码,我在为项目构建的一些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 …
Run Code Online (Sandbox Code Playgroud)