Object Oriented Programming - Set the Child's Prototype to an Instance of the Parent

Tell us what’s happening:
Hi everyone.

There are some details I don’t seem to quite understand concerning inheritance, thus why I tried to code the following:

Your code so far

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

function Dog() {
  this.color = "grey";
}

function Cat() {
  this.domestic = true;
}

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

Dog.prototype = Object.create(Animal.prototype);
Cat.prototype = Object.create(Animal.prototype);

let beagle = new Dog("Fido");

console.log(beagle.eat()); 
// works like a charm --> output:"nom nom nom"
console.log(beagle.name); 
// output: "undefined"

I wanted to avoid redundancy as much as possible. So, I thought it would be a good idea to add the “name” parameter to “Animal” instead of “Dog” or “Cat”.

But now I wonder, how do I add the name of a new Dog or Cat object?

let beagle = new Dog("Fido");

The above returns “undefined” with console.log.

Is there a way to solve this?

Thanks in advance

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:106.0) Gecko/20100101 Firefox/106.0

Challenge: Object Oriented Programming - Set the Child’s Prototype to an Instance of the Parent

Link to the challenge:

noob here:
but your code is not calling the Animal function so how would it set a key?
you could set a name in the prototype, but wouldn’t that defeat the purpose of a prototype assuming all animals names are different? as it’s not a function (correct me if i’m wrong) so you can’t pass an argument.

not sure where the redundency is. your dog has its own name but eats like other animals.

1 Like

I see. Thank you ugwen.

If I understand this right: “Dog” and “Cat” inherited from the “Animal” prototype, but not from its constructor, which is then normal behavior that new “Dog”/“Cat” objects don’t have the “name” property.

Is then there a way that the Dog- and Cat-constructor inherit from the Animal constructor, so that I don’t have to repeat for each (Dog and Cat) the “name” property and argument?

Thanks in advance

Edit:

The solution was right under my nose…

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

function Dog() {
  constructor = Animal; // <---
  this.color = "grey";
}

function Cat() {
  constructor = Animal; // <---
  this.domestic = true;
}

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

Dog.prototype = Object.create(Animal.prototype);
Cat.prototype = Object.create(Animal.prototype);

let beagle = new Dog("Fido");


console.log(beagle.eat());
console.log(beagle.name);

Yes, you’re right. My main focus was to understand how this aspect of inheritance works. It’s true: I could’ve chosen a better example for this. My bad…

Thanks for your help.

i didn’t get that to work, even passing the argument:

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

function Dog(name) {
  constructor = Animal(name); // <---
  this.color = "grey";
}
....

although i’m happy just to stick to the prescribed method and not try to pass key/values for name that way. I see it as large groups and sub-groups and i know how to set common key/values to the sub-groups via the constructor/prototype. any key/values not common to the large group don’t go in the large group/prototype.

my opinion is noob tho, so pinch of salt n’ all that.

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