Given this code:
function init() {
var id = 1234;
$("button").click(function() {
alert(id);
});
}
Basically when init
is called it adds a click
function on to the button
.
Now lets say the button
gets removed from the DOM somehow by external code. Normally, from what I understand, the garbage collector will check if the click
function can be removed as well.
Will the garbage collector fail to remove the click
function since the function is referencing the id
variable via a closure, hence creating memory leak or will the click
function be removed as well together with the DOM element?