Inheritance so you don't repeat yourself

https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/object-oriented-programming/use-inheritance-so-you-dont-repeat-yourself

In this exercise we remove a duplicate describe method from each of cat and bear, and place it a single time in animal.protoype…

This code passes but I can’t see anything in this code that links the Animal.prototype properties to the Bear.prototype and the Cat.prototype objects. So how is this possible?

function Cat(name) {
  this.name = name; 
}

Cat.prototype = {
  constructor: Cat

  }

function Bear(name) {
  this.name = name; 
}

Bear.prototype = {
  constructor: Bear

}

function Animal() { }

Animal.prototype = {
  constructor: Animal,
    eat: function() {
    console.log("nom nom nom");
}
}

Yes, that’s a bit confusing. It isn’t possible, is the answer. Just do what it asks for the challenge, the next challenge has you actually connect the two up.

It may be worth raising an issue on the Free Code Camp curriculum repo: you’re not the first person to point this out, and the description and/or the title maybe needs to be changed to indicate that this doesn’t dry the code up at all, that the exercise is just preparation to actually add inheritence. Even if the title was just “Use Inheritance, Part 1” and had a line at the bottom of the description saying “and in the next challenge you actually join these up” or whatever

1 Like