4

I am creating an application in which I have a page for creating a customer. for that I have written following code.

customer=new MobileApp.CustomerViewModel(); //for creating new customer

I want to delete this object. how can I perform this ??

3
  • delete variableName ; Commented May 20, 2013 at 5:34
  • 2
    Note that JavaScript's delete keyword is NOT for deleting contents from memory, it is only for removing properties from within an object. Read the docs. Note that delete x; Will attempt to delete a property x that exists in the global namespace object. Commented May 20, 2013 at 5:37
  • is it a global variable or a local one Commented May 20, 2013 at 5:37

3 Answers 3

17

Setting customer = null will make this enable for garbage collector, given that there is no other valid reference to that object.

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

Comments

3
delete customer;

See about delete

delete operator removes a property from an object. As customer is a property of the global object, not a variable, so it can be deleted

Note : customer should be a global one

customer=new MobileApp.CustomerViewModel();
delete customer; // Valid one

var customer1=new MobileApp.CustomerViewModel();
delete customer1; // Not a valid one

Sample Fiddle

1 Comment

This doesn't answer the question, and delete doesn't affect memory: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
3

Recursively destroy the object as shown below. Tested with chrome heap snapshot that the javascript objects are getting cleared in memory

function destroy(obj) {
    for(var prop in obj){
        var property = obj[prop];
        if(property != null && typeof(property) == 'object') {
            destroy(property);
        }
        else {
            obj[prop] = null;
        }
    }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.