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?
deletekills the index in the array and there remains a hole in it - Grundy