function addHandler() {
var el = document.getElementById('el');
el.onclick = function() {
this.style.backgroundColor = 'red';
}
}
The code above was included in a Mozilla blog post on JavaScript and states that the above code causes a memory leak.
Could someone explain it more than:
Because the reference to el is inadvertently caught in the closure created for the anonymous inner function. This creates a circular reference between a JavaScript object (the function) and a native object (el).
Thanks!