Why when I pass childObject
directly and change it, object.childObject
stays the same. While passing object and changing object.childObject
does change object.childObject
outside the function.
In my head I always pass by reference, so if I change the value of what childObject
points to in f_b()
, shouldn't it also change it in object.childObject?
If I pass child object:
f_b(someObject) {
someObject = {
b: 2
}
console.log("childObject inside f_b is ", someObject)
}
f_a() {
let object = {
childObject: {
a: 1
}
}
this.f_b(object.childObject);
console.log("childObject inside f_a is ", object.childObject)
}
f(a)
Running F(a) would output:
childObject inside f_b is {b: 2}
childObject inside f_a is {a:1}
If I pass parent object:
f_b(someObject) {
someObject.childObject = {
b: 2
}
console.log("childObject inside f_b is ", someObject)
}
f_a() {
let object = {
childObject: {
a: 1
}
}
this.f_b(object);
console.log("childObject inside f_a is ", object.childObject)
}
Running F(a) would output:
childObject inside f_b is {b: 2}
childObject inside f_a is {b: 2}
Why doesn't the former output the same as the latter?