這是Pet
代碼:JS對象,需要的解釋發生了什麼,以及爲什麼
function Pet() {
this.vitals = new Vitals();
}
function Vitals() {
this.hunger = 5;
this.thirst = 0;
this.emotions = {
happy: true,
sad: true
}
}
下面是調用等等:
$(document).ready(function() {
var pet = new Pet();
console.log(pet);
pet.vitals.emotions.happy = false;
console.log(pet);
});
這是我從控制檯獲取:
曲estion:爲什麼happy
false
在兩個讀數中,而不僅僅是第二個?
請參閱[Chrome的JavaScript控制檯是否懶惰地評估數組?](http://stackoverflow.com/q/4057440/218196) – 2014-11-04 02:51:02