I have someone's code which sets the name of a new Object.
function setName(human) {
human.name = "Nicholas";
human = new Object();
human.name = "Greg";
};
var newPerson = new Object();
setName(newPerson);
console.log(newPerson.name);
I want to use the value of the property 'name' of the person object.
I was expecting the value to be 'Greg' but I am getting 'Nicholas'.
This is my understanding: 'human' is a pointer to an Object. When the function is called, another pointer to the same Object would be created by the name of newPerson.
The 'name' property of the newPerson Object would be set to 'Nicholas'. Then, another newPerson object is created in the second line of the function. Hence, the first instance should be destroyed. Then, the 'name' property of this new Object would be set to 'Greg'. Where am I going wrong?