nvdstruis
nvdstruis

Reputation: 33

How to delete object with a prototype

I am trying to delete an object that was created with a prototype object. The delete seems to not be working. After the call to delete, the object is still there... The object is created by calling:

utils.createWithProto = function (o) {
    function F() {}
    F.prototype = o;
    return new F();
};

The following below is used to create the object.

// Create a new object using DashboardControl as the prototype
// A unique ID is set in newControl.config.controlId if it was not passed in config
var proto = new DashboardControl(config);
var newControl = Utils.createWithProto(proto);

// Create a collection of Volume models
newControl.volumeCollection = new VolumeCollection();
newControl.volumeCollection.init(newControl.config);

// Create a view
newControl.volumeView = new VolumeView(newControl, newControl.volumeCollection);

I'm manually calling delete on the volumeCollection and volumeView items, that works fine. But the delete of the object itself as a last step is not working:

deleteObject: function(object) {
    object.volumeCollection.stop();
    delete object.volumeCollection;
    object.volumeView.stop();
    delete object.volumeView;
    //delete object.config;
    delete object.prototype;
    delete object;
    object = null;
}

How do I go about deleting this derived object?

Upvotes: 1

Views: 3096

Answers (3)

loxxy
loxxy

Reputation: 13151

This is a case where delete works :

var b = [1,2,3];

delete b[2];          // b becomes [3, 4, undefined]

When you want to clear memory used by b, you simply re-assign it to nothing.

 b = null;

Browser's garbage collector will take care of it from there on.

Upvotes: 1

Juned Ahsan
Juned Ahsan

Reputation: 68715

If the property is an object reference, the delete command deletes the property but not the object. The garbage collector will take care of the object if it has no other references to it.

Upvotes: 1

Quentin
Quentin

Reputation: 943981

delete will only delete properties on an object. If you want to delete a value on a variable, then let it go out of scope or assign a new value to it.

Once all the properties and variables referencing an object have gone, the object will be garbage collected.

Upvotes: 3

Related Questions