I understand how objects are passed by reference in Javascript. In the following code my function changes a property of an object. It changes in the global scope as expected. I understand it.
let a = {name:'blah1'};
function something(obj1) {
obj1.name = 'blah2';
console.log(obj1); // prints {name:'blah2'}
}
something(a);
console.log(a); // prints {name:'blah2'}
In the following code, I rather pass another object to my function and make my first object equal to the second. This time it does not change in global scope. How can I understand this?
let a = {name:'blah1'};
let b = {name:'blah2'};
function something(obj1, obj2) {
obj1 = obj2;
console.log(obj1); // prints {name:'blah2'}
}
something(a,b);
console.log(a); // prints {name:'blah1'}