Simple plugin:
(function ($) {
var MyTest = function (){
this.bigarr = new Array(20000);
this.init();
};
$.fn.extend({
mytest: function(options){
return this.each(function() {
new MyTest();
});
}
});
$.extend(MyTest.prototype, {
init: function(){
console.log(this);
}
});
})(jQuery);
开发者_运维技巧Then i attach the plugin to a DOM element:
$('#selector').mytest();
and remove the element:
$('#selector').remove();
What happens with the plugin? Does the big array still exist in memory? Or do I have to remove it manualy?
Calling remove on a jQuery selector removes the element from the DOM tree, but not from memory.
See this answer for more discussion.
ian.
Considering that you never assign an instance of MyTest to a variable, it can be immediately garbage collected. The memory can be reclaimed whether or not you remove the element.
精彩评论