Object Oriented Programming - Add Methods After Inheritance

Tell us what’s happening:
I know how to solve this, by adding Prototype properties individually, as shown in the suggested solution. But, what if I want to add all properties at once to a new object, that already contains the properties, as shown bellow? This way Dog is not inheriting the eat() method from Animal. But I dont understand why. Is it possible to do something similar?
Thank you.

Your code so far

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

function Dog() { }

// Only change code below this line

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

Dog.prototype = {
  constructor: Dog,
  bark: function() {
  console.log("Woof!");
        },
}

// Only change code above this line

let beagle = new Dog();

Your browser information:

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

Challenge: Object Oriented Programming - Add Methods After Inheritance

Link to the challenge:

You are reassigning Dog.prototype to different value that’s why it doesn’t retain old values

Dog.prototype = Object.create(Animal.prototype); first here 

Then here

Dog.prototype = {
  constructor: Dog,
  bark: function() {
  console.log("Woof!");
        },
}
1 Like

Thank you.
Now I understand. It is so simple.
So, if I want to maintain inheritance (i.e. not reassigning the object), every time I want to add multiple properties, I have to add them individually? Is there a way to add multiple properties at once and keep inheritance?

Since Animal only has one method/property, then I do not see why you would even execute the following if you do not want to inherit the eat method from Animal.

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

I do want to inherit the eat method. And also I want to add multiple properties at once. Is there a way?
Thank you.

If you do not want to inherit the only property/method of Animal, then you would just create a Dog function and define its prototype with using trying to create one based on Animal’s prototype. Basically, you are not wanting to use inheritance at all.

Sorry. I don’t understand. I have already said that I want to inherit the eat method from animal. Why are you saying that I dont want that? In this example there is just the eat method but let’s consider that there are various methods.
So, I have an Animal (supertype) with some methods that are common to all animals. When I create a subtype of Animal (for example dog or bird or other) I want it to inherit the methods from the supertype, and then, I want to add various properties that are specific to that subtype, all at once, without having to add each property individually. Imagine I want to add 20 properties to the subtype. That would take to long, if I have to write a line of code for every propertie.
Is there a way to achieve what I described?
Again, I apologize if I don’t understand what you said.
Thank you.

I’m not sure if there is a way with that (old) syntax for your problem, but later in the Object Oriented Programming part of the course you learn more about this concept, and it’s more detailed. So don’t worry about it now