I have the following code:
for (var i=0; i < 20; i++) {
var itemButton = $('<button />').click(function(){ alert('Hello'); }).append('Hello');
$('#container').append(itemButton);
}
Where i create lots of buttons each with a onclick function. Then i clear the #container using $('#shopSearchTopBar').empty(); and create the buttons again.
My question is: Is this a possible memory leak? Does Javascript frees the memory used for those anonymous functions i created the first time when i call the .empty() method on the #container or the memory gets freed an i can safely add those 20 new buttons each with his own new anonymous function?
bindandclickfor assigning handlers..click(fn)is just a wrapper for a call to.bind('click',fn), so I don't understand how the bind version shortens this code. There are other uses of bind where it can shorten a bit, but they don't really relate to the question.