このコードを理解するのを手伝ってください。
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']);
出力が になると予想してAnother person designation: Designation, person designation: undefined
いましたが、驚いたことに、 であることがわかりました`Another person designation: Designation, person designation: Designation
。
私によると、オブジェクトをanotherPerson
拡張しておりperson
、に設定されたプロパティはオブジェクトanotherPerson
に表示されるべきではありませんperson
。私はここで間違っていますか?それとも、両方のオブジェクトが同じ場所を指しているのでしょうか?
[編集]
今、さらに多くの驚きがあります。
上記に次のコードを追加しました。
person.place = 'XYZ';
console.log(person['place'] + ', ' + anotherPerson['place']); // Expected: XYZ, undefined. Result: XYZ, XYZ.
上記の結果と回答に基づいて、両方のオブジェクトが同じ場所を参照していると思いました。今、さらにいくつかの行を追加しました
person = undefined;
console.log(anotherPerson['place']) //Expected: error, Result: XYZ. ??!?!?
console.log(person['place']) // Expected: error, Result: error.
誰かがこれを理解するために私に光を当てることができますか? 事前にご協力いただきありがとうございます