开发者

does jquery plugin deleted when DOM element removes

开发者 https://www.devze.com 2023-03-15 06:38 出处:网络
Simple plugin: (function ($) { var MyTest = function (){ this.bigarr = new Array(20000); this.init(); }; $.fn.extend({

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消