When rewriting previously written code, it was necessary to keep in memory all instances of the class. All class instances are stored as a static property (string *) (if you can call it that) in the form of an array, which is updated with a new element when creating a new instance of a class. The code is as follows:

class Child { constructor(name) { this.name = name; (Child.__instances__ = Child.__instances__ || []).push(this); // * } } class Doughter extends Child { constructor(name) { super(name); } } class Son extends Child { constructor(name) { super(name); } } class DifferentClass { constructor() { } initializeChildren() { this.son = new Son("Ваня"); this.doughters = [new Doughter("Маня"), new Doughter("Аня")]; } removeVanya() { console.log("Ваня помирает"); this.son = null; } } let obj = new DifferentClass(); obj.initializeChildren(); obj.removeVanya(); console.log(Child.__instances__); 

If you execute this code, you can see that, even though there is no son anymore in the obj object, the instance is still in the __instances__ array. And, as is known from the already given answers , an instance of the class exists exactly as long as it is at least referred to from somewhere.

I decided to follow the path of least resistance and directly remove the necessary (or rather unnecessary more) element, which, for obvious reasons, was not crowned with success:

 removeVanya() { console.log("Ваня помирает"); Child.__instances__[this.son] = null; this.son = null; } 

Actually, the question is: how to delete the necessary element in the array __instances__ , so that this instance is not contained anywhere else?

  • I work with classes rather trivially, and such problems did not appear, but why not use the delete operator? - DimenSi
  • @DimenSi, delete kills the index in the array and there remains a hole in it - Grundy

1 answer 1

field __instances__ - is an array. To remove an element from an array, you can use the splice method. For this you need to know the index of the element to be deleted. You can determine the index using the indexOf method

As a result, the delete function can take the form:

 removeVanya() { console.log("Ваня помирает"); var childIndex = Child.__instances__.indexOf(this.son); if(childIndex != -1) Child.__instances__.splice(childIndex,1); this.son = null; } 

In the current code, when you call Child.__instances__[this]=null , this converts this to a string, most likely [Object object] , and assigns a null value to the property of the same name in the array.

  • And where can I read what is __instances__ ? - DimenSi
  • @DimenSi is an array storing all instances of a given class in itself, in the question code * marked. - smellyshovel
  • It seems that your code does not work. Not to be mistaken, copied and pasted your code. Either I miss something, or it really does not work. - smellyshovel
  • Oh, right. Must be indexOf(this.son) , not just indexOf(this) . And yes, I also had to have Child.__instances__[this.son]=null in the code. And then the behavior is exactly as you said. - smellyshovel
  • @smellyshovel, yes, for some reason, did not pay attention to what this.son was supposed to be. Corrected - Grundy