Why do we need Prototypes


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() {

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


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


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


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