Why do we need Prototypes


#1



Why do we need to add a particular method "bark" to the Dog class using prototypes.

Cant we directly add 'bark' method to our Dog constructor as follows:
function Dog(breed) {
this.breed = breed;
this.bark = new function() { console.log("whoof");};
}


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

yep, this is always a conflict. Codecademy sometimes teaches concepts (like prototype) which are very useful in some cases, but are not the most efficient solution in the lesson.

so, this is about teaching a concept


#3

Well they are part of the basics so it's an add to know they exist :slight_smile:


#4

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