Reputation: 463
I'm currently creating a project in which every visible element, is represented by javascript object.
I need a method within this object, which will let me destroy it.
There's an example of that object :
var example = function(some_args){
var self = this;
var references = {}; //this holds references to inputs within given view
this.createView = function(){
//here I`m doing "stuff" like filling innerHTML of container
//creating event delegate, etc.
}
this.destroy = function(){
self.elements["box"].parentNode.removeChild(self.elements["box"]); //box is a reference to container element
self.elements = null;
delete self;
}
Now, my question is : am I doing everythig what I have to in order to COMPLETLY destroy this object? I`m not holding any other references to objects or elements.
EDIT: I see that some of You do not undestand my question. Barmar got it right, for which I am gratefull :).
I`m aware of GC, it is just easier to write "I am destroying object" than "I am removing last reference to object, so GC could take care of it" :)
To be specific. Considering that I am removing last reference to object, inside function which is part of this object - is there anything else I should take care of? Or my code is completly fine, and object will be considered as garbage?
Upvotes: 1
Views: 2475
Reputation: 348
Javascript has a garbage collector, so you don't need to destroy the object. Also you can't
Upvotes: 0
Reputation: 255025
You don't handle lifetime of an object in JS explicitly.
So for the given question:
am I doing everythig what I have to in order to COMPLETLY destroy this object?
the only answer is:
You cannot do that, since an object in JS can only be destroyed by a GC, which you cannot interact with. When an object is reachable - then it's alive. When it's not - then it's a "Schroedinger's object".
Upvotes: 1