This has nothing to do with how your references are passed. It has to do with scope and the fact that your function argument names are the same as your higher scoped variable names.
If yes, then why updating obj2.value is not effecting obj1.value?
It is affecting obj1.value, but only from within the function because you only made the change to the local obj1 variable.
The issue is simply that you have local function argument names that hide duplicate names from the global scope. Any changes you make in your function are referring to newly created local variables and don't affect the global ones. Since you never actually make any changes to the object that obj1 originally pointed to, from within your function, you will never see any changes outside of it either.
Let's walk through this....
Inside your function, obj1 is a new local variable that does start out being a copy of what's in the Global obj1 variable and now there are two obj1 variables that point to the same one actual Global object. But, then you change what the local obj1 variable points to with this:
obj1 = obj2;
So now, there are 3 ways to access the Global obj2 object:
- the Global
obj2 variable (window.obj2)
- the local
obj1 variable (obj1)
- the local
obj2 variable (obj2)
You then make a change to the object that those 3 references point to with:
obj2.value = "new value";
At this point, you have done nothing but modify the object referenced by these 3 variables (the Global obj2 object). The Global obj1 object has not been altered in any way.
So, inside the function, when you access obj1, you are getting a reference to the Global obj2, but when the function returns, all local variables are destroyed and you now go back to having just one way to access each of the two objects in memory. Since you never actually modified the Global object that obj1 points to, you see no change there, but you did modify the object that Global obj2 points to from within the function, so you do see that object modified.
If you had actually modified the Global object that obj1 originally referenced from within the function, you would have seen those changes persist outside of it.
See comments inline:
var num = 10,
name = "Addy Osmani",
obj1 = {
value: "first value"
},
obj2 = {
value: "second value"
},
obj3 = obj2;
function change(num, name, obj1, obj2) {
// All your arguments have the same names as global variables
// Changes to primitives here won't affect the primitives outside
// of the function because copies of those primitives were passed
// into the function and your local variables hold those copies:
num = num * 10;
name = "Paul Irish";
// Let's do the simplest test possible with your object references...
// Just use the local variables to see if they affect the Globals
obj1.value = "Changed from within the function!";
obj2.value = "Me too!";
}
change(num, name, obj1, obj2);
console.log(num); // 10 (the global num)
console.log(name); // "Addy Osmani" (the global name)
console.log(obj1.value); // "Changed from within the function!"
console.log(obj2.value); // "Me too!"
console.log(obj3.value); // "Me too!"