I thought objects are passed as reference. But when I delete b
, it still exists in c
. Please see this example:
This first part makes sense to me as its passed by reference:
var a = {b: {val:true}};
a.c = a.b;
a.b.val = 'rawr';
console.log(uneval(a)); // outputs: "({b:{val:"rawr"}, c:{val:"rawr"}})"
Now this part does not make sense to me:
var a = {b: {val:true}};
a.c = a.b;
a.b.val = 'rawr';
delete a.b;
console.log(uneval(a)); // outputs: "({c:{val:"rawr"}})"
so b
property is deleted, but c
property is holding the properties to what the referenced before delete. is this a bug in javascript?
edit: thanks all to the replies! so its not a bug, and this behavior is actually very good, it allows people to change "key"/"property" names while retaining the object! :)
console.log(a)
instead ofconsole.log(uneval(a))
because it will show you properties and other useful debugging stuff anyway (firebug and chrome console).console.log
you're right, but i did in uneval just to show output in this example. Thanks for the pass-by-value note.