I'm trying to understand a complexity someone showed me in JavaScript. Consider the following simple function:
function myFunction(p) {
p.property = 1;
}
var obj = {property: 0};
console.log(obj); // {property: 0}
myFuncton(obj);
console.log(obj); // {property: 1}
Let's repeat this, but instead of overwriting one part of the object, let's overwrite the whole object:
function myFunction(p) {
p = {property: 1};
}
var obj = {property: 0};
console.log(obj); // {property: 0}
myFuncton(obj);
console.log(obj); // {property: 0}
Why does obj not get replaced here as expected?