Object Oriented Programming - Use Inheritance So You Don't Repeat Yourself

Tell us what’s happening:

My question is, in both Cat.prototype and Dog.prototype, if I change the constructor to Animal instead of Cat and Dog, It still works
Help me to clear my concept

Your code so far

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

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

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

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

function Animal() { }

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

};
Cat.prototype = {
  constructor: Animal
};

Bear.prototype = {
  constructor: Animal
};

Your browser information:

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

Challenge Information:

Object Oriented Programming - Use Inheritance So You Don’t Repeat Yourself

You are reassigning the prototype so they do not have the eat method as an instance method and it is only testing if eat is a property on Cat and Bear.

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

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

console.log(Cat.prototype.hasOwnProperty('eat')) // true

Cat.prototype = {
  constructor: Cat,
};

console.log(Cat.prototype.hasOwnProperty('eat')) // false

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