Confused...in the book page 88-89 you make reference to passing references is actually being passed by value; however, the code that is used to prove this does not make since to me. Inside the function setName(obj) you are creating a new Object reference, which in other languages would say this is a local variable and will die at the end of the function, thus not reflecting a change on the Object created outside the function. Here is a couple of examples:
HTML Code:
var person = new Object();
person.name = 'John';
console.log(person.name);
function changeName(obj){
obj = new Object();
obj.name = 'Nick';
return obj.name;
}
console.log(changeName(person));
console.log(person.name);
This example produces:
John, Nick, John
HTML Code:
var person = new Object();
person.name = 'John Doe';
console.log(person.name);
function changeName(obj){
// obj = new Object(); comment out
obj.name = 'Nick';
return obj.name;
}
console.log(changeName(person));
console.log(person.name);
This example, commenting out the creation of a new Object inside the function, produces:
John, Nick, Nick
Which suggest to me that it was in fact passed by reference and not by value.
Any further clarity on this would be appreciated.
Regards,
Darren