bethrobson / Head-First-JavaScript-Programming

417 stars 345 forks source link

A more interesting implementation of the sit method Question #4

Open SOSANA opened 9 years ago

SOSANA commented 9 years ago

When we call barnaby and spot is the instance object value already set to true automatically even though our prototype is set to false? As I was tracing through trying to understand why the instance object would not be set to false as it inherits from the prototype as it doesn't make sense to me. If it was set to false than the first call would console.log Barnaby is already sitting but if it was true than it would skip this and console.log as show in the example. Scratching my head on this one.

// adding new property for sitting
Dog.prototype.sitting = false;
// adding new method for sit
Dog.prototype.sit = function() { 
    if (this.sitting) {
        console.log(this.name + " is already sitting"); 
    } else {
        this.sitting = true;
        console.log(this.name + " is now sitting"); 
    }
};

barnaby.sit();
barnaby.sit();
spot.sit();
spot.sit();
bethrobson commented 9 years ago

I'm not quite sure I understand the question...

When you call barnaby.sit() the first time it checks to see this.sitting is true. barnaby doesn't have a sitting property so we look in the prototype. sitting is set to false in the prototype, so this.sitting is false, so we execute the else clause, and set this.sitting to true.

When you set this.sitting to true, you're adding a new sitting property to the barnaby instance object! (you're NOT changing the prototype's value for sitting). So now barnaby.sitting is true, but the prototype's sitting value is still false.

Next time you call barnaby.sit(), now barnaby has a property sitting, that's set to true, so you see the message that barnaby is already sitting.

Now call spot.sit(). Spot doesn't have a sitting property, so look in the prototype. The prototype's sitting property is still false!!! So now, when we say this.sitting = true, we are adding a new property sitting to spot, the instance object. So now spot.sitting is true, but the prototype's sitting property is STILL false.

So every time we say this.sitting = true on an instance object, if that instance doesn't yet have a sitting property itself (in other words, it's inheriting its sitting property from the prototype), we ADD a new sitting property to the instance, which overrides the prototype's sitting property. The prototype's sitting property never changes.

Hope that helps!

SOSANA commented 9 years ago

EUREKA when you said this "When you call barnaby.sit() the first time it checks to see this.sitting is true. barnaby doesn't have a sitting property so we looking the prototype. sitting is set to false in the prototype, so this.sitting is false, so we execute the else clause, and set this.sitting to true."

Sorry for the confusing question. The this.sitting in the if statement is what confused me at first, but your answer makes sense now thanks.

bethrobson commented 9 years ago

Excellent