If there are no references remaining to the element after you remove it, the GC should clean up handler functions that it references. So you shouldn't need to do anything.
But if IE has a bug along this way, you can use removeEventListener
to remove the handler, but this requires that you use a named function, since you have to give the same function to removeEventListener
as you did when you called addEventListener
, and anonymous functions will never be the same as each other.
function myClickHandler {
...
}
var li = document.createElement('li');
li.addEventListener('click', myClickHandler);
...
li.removeEventListener('click', myClickHandler);
myClickHandler = null;
li.parentNode.removeChild(li);
myClickHandler = null;
is needed because otherwise the function name will hold a reference to the handler function, so it won't be GCed.
If you have multiple LIs, and they're all using the same handler function, it shouldn't be necessary to do this. No matter how many LIs you have, they're all just referring to the same function, so it doesn't take up lots of memory.
ul
references as a childappendChild
and once I remove child, theclickHandler
will be garbage collected? no references from global.