Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleting Objects in JavaScript

I'm a bit confused with JavaScript's delete operator. Take the following piece of code:

var obj = {     helloText: "Hello World!" };  var foo = obj;  delete obj; 

After this piece of code has been executed, obj is null, but foo still refers to an object exactly like obj. I'm guessing this object is the same object that foo pointed to.

This confuses me, because I expected that writing delete obj deleted the object that obj was pointing to in memory—not just the variable obj.

Is this because JavaScript's Garbage Collector is working on a retain/release basis, so that if I didn't have any other variables pointing to the object, it would be removed from memory?

(By the way, my testing was done in Safari 4.)

like image 677
Steve Harrison Avatar asked Apr 12 '09 23:04

Steve Harrison


People also ask

How do we delete the object property in JavaScript?

The delete operator deletes both the value of the property and the property itself. After deletion, the property cannot be used before it is added back again. The delete operator is designed to be used on object properties. It has no effect on variables or functions.

How do you delete an object?

Right-click over the objects you want to delete, and choose Delete. In the Delete dialog box, select the objects you want to delete from the list.

How do you remove objects from objects?

Answer: Use the delete Operator You can use the delete operator to completely remove the properties from the JavaScript object. Deleting is the only way to actually remove a property from an object.

What is delete in JavaScript?

The JavaScript delete operator removes a property from an object; if no more references to the same property are held, it is eventually released automatically.


2 Answers

The delete operator deletes only a reference, never an object itself. If it did delete the object itself, other remaining references would be dangling, like a C++ delete. (And accessing one of them would cause a crash. To make them all turn null would mean having extra work when deleting or extra memory for each object.)

Since Javascript is garbage collected, you don't need to delete objects themselves - they will be removed when there is no way to refer to them anymore.

It can be useful to delete references to an object if you are finished with them, because this gives the garbage collector more information about what is able to be reclaimed. If references remain to a large object, this can cause it to be unreclaimed - even if the rest of your program doesn't actually use that object.

like image 69
Jesse Rusak Avatar answered Oct 07 '22 22:10

Jesse Rusak


The delete command has no effect on regular variables, only properties. After the delete command the property doesn't have the value null, it doesn't exist at all.

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.

Example:

var x = new Object(); x.y = 42;  alert(x.y); // shows '42'  delete x; // no effect alert(x.y); // still shows '42'  delete x.y; // deletes the property alert(x.y); // shows 'undefined' 

(Tested in Firefox.)

like image 22
Guffa Avatar answered Oct 07 '22 21:10

Guffa