Reputation: 32107
(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.
Upvotes: 0
Views: 284
Reputation: 29
That's not a memory leak. obj.c only holds a copy of the value assigned to obj.a.
Upvotes: 1
Reputation: 64399
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/
Upvotes: 0