Is there a way to cleanup things within a class before it's destroyed by the garbage collector like the del method in Python classes? This is mainly for DOM objects that have been created within the class? I think when the class is destroyed, the pointer to that DOM object will be destroyed, too, but the DOM element itself will stay in existance. As an example:
class Test {
private div: HTMLDivElement;
constructor() {
this.div = document.createElement("div");
document.body.appendChild(this.div);
}
__del__() {
this.div.parentNode.removeChild(this.div);
}
}
I think if I do the above methode I still need to explicitly run the del methode before destroying the instance, which can be annoying. I could not find any info about methods that are automatically run on destruction. What is the best approach here?