Use of Inheritance So I Don't Repeat my Code

Use of Inheritance So I Don't Repeat my Code
0

#1

Tell us what’s happening:
I have written this code and it works FINE! But the problem is that I don’t understand what’s the use of putting the eat() in the ‘Animal’ object’s prototype.

I mean, I’m not inheriting Animal in Cat and Bear object constructor function (or may be cause I don’t know how). :thinking:

– If you understand this code please let me know how its working.
:persevere::pray:

Your code so far



function Animal() { }

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

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

Cat.prototype = {
  constructor: Cat
};

Bear.prototype = {
  constructor: Bear
};

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36.

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


#2

Oh! You’re in a rush. That’s what the next challenge will teach you :sunny: