-
Notifications
You must be signed in to change notification settings - Fork 346
A more interesting implementation of the sit method Question #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
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! |
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. |
Excellent |
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.
The text was updated successfully, but these errors were encountered: