Please help me in understanding this piece of code.
var person = {
'first-name': 'FirstName',
'last-name': 'LastName',
'gender': 'Male'
};
var anotherPerson = new Object(person);
anotherPerson.desig = 'Designation';
console.log('Another person designation: ' + anotherPerson['desig'] + ', person designation: ' + person['desig']);
I expected the output to be Another person designation: Designation, person designation: undefined
but to my surprise I found it to be `Another person designation: Designation, person designation: Designation
.
According to me anotherPerson
is extending person
object and properties set to anotherPerson
should not be visible to person
object. Am I wrong here? Or is that both the object are pointing to the same location?
[EDIT]
Now there are even more surprises.
I added the following code to the above.
person.place = 'XYZ';
console.log(person['place'] + ', ' + anotherPerson['place']); // Expected: XYZ, undefined. Result: XYZ, XYZ.
Based on the above result and answers I thought that both objects are referring to the same location. Now I added few more lines
person = undefined;
console.log(anotherPerson['place']) //Expected: error, Result: XYZ. ??!?!?
console.log(person['place']) // Expected: error, Result: error.
Can someone throw some light on me to understand this? Thanks for your help in advance