Reputation: 1329
I'm writing a destroy method for a jQuery plugin. I'm concerned about the potential for memory leaks caused by references to DOM objects persisting after the destroy method has been called. Does the code below have potential to cause a memory leak?
jQuery.fn.foo = function(){
var $bar = $('.bar');
$bar.on('click.foo', function(){
var $baz = $('.baz');
});
this.destroyFoo = function(){
$bar.off('click.foo');
//is this necessary to avoid an orphan node?
$bar = null;
};
return this;
};
var $qux = $('.qux').foo();
$qux.destroyFoo();
Upvotes: 0
Views: 105
Reputation: 1302
Looks fine to me. Your variable $bar is declared within the function scope so it won't be accessible outside of it.
$bar = null; - is enough to clear the reference to your element.
Upvotes: 1