These two simple examples were given to me to explain objects in javascript. I understand the first example, but what eludes me is why in the second (highly similar) example o isn't overwritten with an empty object?
function foo(obj) {
obj.x = 7;
}
var o = {x: 5};
foo(o);
console.log(o.x); // 7
It seems to me that in the following example, undefined should be logged:
function foo(obj){
obj = {};
}
var o = {x: 5};
foo(o);
console.log(o.x); // 5
But it isn't, how come? What is the difference between these two examples, that causes o not to be overwritten in the second example?