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 ??
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 ??
Setting customer = null
will make this enable for garbage collector, given that there is no other valid reference to that object.
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
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;
}
}
}