14. Prototype To The Rescue - Question


#1


https://www.codecademy.com/en/courses/objects-ii/2/4?curriculum_id=506324b3a7dffd00020bf661


I just want to confirm, that in addition to using prototype in the below manner, we could also just add the bark method to the constructor, right? And all objects within the class would be able to use it.


function Dog (breed) {
  this.breed = breed;
};

// here we make buddy and teach him how to bark
var buddy = new Dog("golden Retriever");
Dog.prototype.bark = function() {
  console.log("Woof");
};
buddy.bark();

// here we make snoopy
var snoopy = new Dog("Beagle");
/// this time it works!
snoopy.bark();


#2

Are you implying to put it in the main object prototype?


#3

Hey Bandit,

Yes, I believe so. If I created a constructor (ie. Person) and added a speak method to it then couldn't each person I created using the constructor use the speak method?

Thanks.


#4

Hi Kevin,

Only if that Person constructor was the Parent or higher then the other constructor trying to inherit from it. :slight_smile:


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.