Why can't I use an object "add methods after inheritance"?

Tell us what’s happening:
Why can’t I use an object “add methods after inheritance”?

I thought I could in the previous lessons.

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!");
}
}
let beagle = new Dog();
console.log(beagle.constructor);




Your browser information:

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

Challenge: Add Methods After Inheritance

Link to the challenge:

When you write:

Dog.prototype = { }

you are redefining the prototype and overwriting what you did in the previous line:

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

If you notice, the tests you are failing relate the to the Animal prototype. Look at the Bird example again to see how you retain the Animal methods while defining a new constructor for Dog while adding a new bark method for Dog.

1 Like