1

This is the code for Pet:

function Pet() {
    this.vitals = new Vitals();
}

function Vitals() {
    this.hunger = 5;
    this.thirst = 0;
    this.emotions = {
        happy: true,
        sad: true
    }
}

Below is the calling and so forth:

$(document).ready(function () {
    var pet = new Pet();
    console.log(pet);
    pet.vitals.emotions.happy = false;
    console.log(pet);
});

This is what I get from the console:

console results

Question: Why is happy false in both readouts, and not just the second one?

Matt
  • 1,500
  • 8
  • 23
  • 38
  • 2
    See [Is Chrome's JavaScript console lazy about evaluating arrays?](http://stackoverflow.com/q/4057440/218196) – Felix Kling Nov 04 '14 at 02:51

1 Answers1

1

Probably because console.log can update data already printed if the underlying object reference is updated afterwards. Try to create another reference called pet2 to see the trick.

By the way, you should to check this and this.

Community
  • 1
  • 1
Mik378
  • 21,881
  • 15
  • 82
  • 180
  • 1
    Interesting. Someone suggested stringifying in that link and that seems to capture the state of execution, rather than it's current state, so that link was quite helpful. :) – Matt Nov 04 '14 at 02:46
  • Indeed, the subject is interesting :) – Mik378 Nov 04 '14 at 02:47