Can someone scratch the itch for me regarding the nature of the zero-based fix used to prevent memory leaks?
We are all familiar with the following method to stop the circular reference between the DOM object and the JS object in order to prevent memory leak:
function foo() { var ele = document.getElementById("someParagraphId"); ele.onclick = function() {
The question is, why does this work? Setting "ele" to null will certainly stop circular links, but won't this also interfere with future links to "ele"?
function foo() { var ele = document.getElementById("someParagraphId"); ele.onclick = function() { console.log("accessing ele ... after set to null " + ele.onclick); }; ele = null; }
Still, the event is being tapped. He will complain that the "ele" object is null (this is what we expect).
Given the above behavior, can we conclude that the implementation of the Javascript mechanism will contain some internal link to the event listener, and that this link is called when the event fires?
eventHandlerRef = //assignment to "ele.onclick" handler/listener;
If there was a link like the one above, would it not be dependent on the implementation of assignment-to-null? Or is it part of the ECMAScript specification.
As far as I understand, this fix has always been safe for the browser. I have not come across many examples that contain specific references to detecting / sniffing a browser type before applying a null destination.
================ EDIT ==================
I think that, due to the way I posed this question, perhaps involuntarily, a direct discussion of what I tried to convey. A couple of concepts that are referenced:
object references / object references ala:
var obj1, obj2; obj1 = {foo: "bar"};
This is not the place where my itch lies, and I regret to add the line:
console.log("accessing ele ... after set to null " + ele.onclick);
The above makes this question closed. I fully expected the error to be thrown as indicated in the original post.
My itch is more about context ... for some reason, in my opinion, I keep thinking that the Javascript mechanism will call ele.onlick() directly when the event fires and sets the element to zero, akin to the following thread:
var obj = {}; obj.method = function() {}; obj = null;
Considering that in the original message we know that the event handler still fires after the element is set to null, in my opinion the stream is more akin to it:
var obj = {}; obj.method = function() {}; var objRef = obj; obj = null;
My question is how most Javascript works when some kind of internal link points to an assigned event handler, and that internal link is called when the event fires?
Or, in other words, what stops the implementation of the Javascript Engine from calling ele.onclick() rejecting design and architecture problems at the moment)?
Perhaps my thought processes work differently, but no one repeated the second glance when they first encountered the -null-null assignment, where the element reference was null and still the code for the handler was still executing?