Please explain why changing a global objects string property in local scope does affect the property in a global scope but giving a new value to the whole object doesn't.
In my example, the first time I run changeMyName function, I get what I'm expecting - a change in my_global_object.name value.
But why doesn't the second time I run the function change the my_global_object object to my_global_object.children.first_child object?
var my_global_object=new Object();
my_global_object.name='my parent name'
my_global_object.children=new Object();
my_global_object.children.first_child=new Object();
my_global_object.children.first_child.name='my first child name';
function changeMyName(child_name,new_name){
var my_local_object;
my_local_object=my_global_object;
if(child_name) my_local_object=my_local_object.children[child_name];
my_local_object.name=new_name;
}
changeMyName(false,'new parent name');
changeMyName('first_child','new first child name');
Why is this "=" assignment
if(child_name) my_local_object=my_local_object.children[child_name];
different from this "=" ?
my_local_object.name=new_name;
Is there a some sort of a "giving a value" and "passing a refrence" difference?