Please explain the output of the following code - bit confused with Javascript pass-by-reference.
var foo = {
n: 1
};
var bar = foo;
foo.x = foo = {
n: 2
};
console.log(foo); //{n:2}
console.log(bar); // {n:1, x:{n:2}}
Please explain the output of the following code - bit confused with Javascript pass-by-reference.
var foo = {
n: 1
};
var bar = foo;
foo.x = foo = {
n: 2
};
console.log(foo); //{n:2}
console.log(bar); // {n:1, x:{n:2}}