0
(function () { //create object with two initail properties var obj = {}; obj.a = { a1: 1, a2: 2 }; obj.b = { b1: 1, b2: 2 }; //create a new property 'c' and refer property 'a' to it obj.c = obj.a; //cyclic reference obj.a = obj.b; obj.b = obj.a; //this function removes a property from the object // and display all the three properties on console, before and after. window.showremoveshow = function () { console.log(obj.a, '-----------a'); console.log(obj.b, '-----------b'); console.log(obj.c, '-----------c'); //delete proprty 'a' delete obj.a; //comes undefined console.log(obj.a, '-----------a'); //displays b console.log(obj.b, '-----------b'); //still displays the initial value obj.a console.log(obj.c, '-----------c'); } })(); 

Now: After deleting obj.a and checking the value of obj.c we find that obj.c still refers to the initial value of obj.a, however obj.a itself doesnt exist. So is this a memory leak. As obj.a is deleted and its initial value still exist.

Edit: is this means,like although we removed the property(obj.a) its values exist even after. Which can be seen in obj.c.

1
  • It is not a leak. the object doesn't 'live" in the variable, all object in javascript are references. The object gets garbage collected when the last reference to it dies, as long as you have any reference to it, it lives. Commented Jan 15, 2011 at 11:01

2 Answers 2

1

That's not a memory leak. obj.c only holds a copy of the value assigned to obj.a.

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

Comments

0

Delete only removes the reference, so that's what happens. Complete answer also here:

Deleting Objects in JavaScript

And if you have some time on your hands, check this :)

http://perfectionkills.com/understanding-delete/

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.