1

I'd like to know how to delete an object from inside, when this object will no longer be used.

For example :

var MyObject = function () {
    return {
        'some': function() {...},
        'actions': function() {...},
        'destroy': function() {
            this = null; // throws a "ReferenceError: Invalid left-hand side in assignment"
        },
    }
}

// Doing so, I would be able to :
var obj = new MyObject();
obj.some();

// And when I have finished :
obj.destroy();

The reason behind that is that I can't destroy it from outside (the object is created on "click" on a DOM element, but it will be destroyed when you click somewhere else, so the scope of the creation (in the "onClick" method) is not available where I want to delete it.

How can I do that?

2
  • 1
    can you try delete this Commented Apr 5, 2013 at 6:03
  • 1
    In fact, no, I just saw a duplicate of my question : stackoverflow.com/questions/2304860/… (that will answer your question) Commented Apr 5, 2013 at 6:04

1 Answer 1

1

JavaScript simply doesn't work that way. In fact, you can't destroy objects whatsoever. The one and only thing you can do is to make sure you do not have any references to an object which will then make it eligible for garbage collection by the JS runtime. If you post a more complete snippet we can tell you whether you have a memory leak or not, but if you have done a corresponding .off for every .on and set any referencing variables to null, that is the full extent of what you can and should do in JavaScript.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.