最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to destroy the instance of a JavaScript class completely? - Stack Overflow

programmeradmin6浏览0评论

I have a class with many methods in its prototype and I want to destroy its instance after it's done.
This answer tells how to destroy an instance of a class but I'm not sure which variables and/or other types of references I may have in the instance and it's methods that prevents it from removing by garbage collector.

I know I need to remove closures like the functions passed to setInterval. What would be a list of possible items I need to do to destroy the instance pletely?

I have a class with many methods in its prototype and I want to destroy its instance after it's done.
This answer tells how to destroy an instance of a class but I'm not sure which variables and/or other types of references I may have in the instance and it's methods that prevents it from removing by garbage collector.

I know I need to remove closures like the functions passed to setInterval. What would be a list of possible items I need to do to destroy the instance pletely?

Share Improve this question edited May 23, 2017 at 12:00 CommunityBot 11 silver badge asked Sep 3, 2015 at 20:10 ReyraaReyraa 4,3043 gold badges30 silver badges57 bronze badges 4
  • setInterval will keep running until you call clearInterval, so call clearInterval when you are done with it. – Matt Burland Commented Sep 3, 2015 at 20:12
  • 3 Did you already check out this? – David Commented Sep 3, 2015 at 20:14
  • I don't think It will destroy its prototype methods by clearing out the instance, if that's what you are after. – MinusFour Commented Sep 3, 2015 at 20:16
  • Thank you @David, But I think setTimeout is not the only reference affecting garbage collector's work. – Reyraa Commented Sep 3, 2015 at 20:49
Add a ment  | 

2 Answers 2

Reset to default 3

These are items I know you should do:

  1. All the functions passed to setInterval and setTimeout must have declaration so you can remove them by making equal to null.
  2. Use clearInterval and clearTimeout for all of them.
  3. All the closures inside your methods must have declaration - must not be anonymous - in order to be removed.
  4. Make sure you haven't used global variables in so that your methods and closures retain the reference to it.
  5. in the last step make the instance itself equal to null.

hope it covers all you need.

Set interval will continue running because it can not free the instance (of A) due to a closure to the A function which is the constructor of the class. Therefore you need to create a dispose pattern in order to free all the resources that the garbage collector can not free. After that the instance would be freed by the garbage collector when it is not used anymore.

function A() {  
   this.timerId = setInterval(function(){ 
       console.log("A"); 
   }, 2000)
}

A.prototype.dispose = function() {
  clearInterval(this.timerId);
};

var instance = new A();

/* Will not work
instance = null;
*/

instance.dispose();
instance = null;    
发布评论

评论列表(0)

  1. 暂无评论