I’m aware that js has garbage collection that frees up memory that are no longer referenced, but I’m curious as to wether the G.C. works on objects that reference them selves like this:
var x = {
b:function(){
alert('hello');
},
y:function(){ //init listeners
$(some-dom-el).click(function(){
x.b();
});
},
z:function(){
var ex = this.r(55,9000);
},
r:function(a,b){
return a + b;
}
}
$(function(){
x.y();
});
so would using delete on var x successfully remove the object (after manually removing the event listeners of course)?
The GC does know how to handle cyclic references. (except in older IEs).
However, if you have a reference in an event handler in a live DOM element, your object will not get GC’d, since that reference will live forever. (or until your remove the handler or the element)