javascript - Setting an Instance to null -
so i've read through javascript memory management , few other articles, understand there garbage collecting daemon runs on interval , auto-magically cleans up.
the problem have if set object instance null
, run timeout
check null after setting null
instance still exists.
here problem using simple example:
class animal{ constructor(name){ this.name = name; } } class cat extends animal{ constructor(name,type){ super(name); this.type = type; } renderhtml(){ console.log(`rendering html...`); //variable exists while block executing... let divelement = document.createelement('div'); //this should reference.... divelement.addeventlistener('click',this.testinstancelife.bind(this)); divelement.innerhtml = this.name; document.body.appendchild(divelement); //remove dom interface.... document.body.removechild(divelement); console.log(`there should 0 references instance, since divelement should removed after block execution..`); } testinstancelife(){ console.log(this); } } //init (()=>{ document.addeventlistener('domcontentloaded',()=>{ var cat = new cat('grumpy','tabby'); cat.renderhtml(); cat = null; console.log(`this object now: ${cat}`); console.log(`finished executing...`); }); })();
question: why async timeout , or instance method still have reference though set null
before both executed?
Comments
Post a Comment