The exercise makes us setting the Dog.prototype to Animal.prototype. But now “beagle” object is like directly inherits from Animal.prototype and Dog.prototype is pretty useless. (It doesnt have any methods of its own)
Previous examples told us its a good practice to set the prototype to an object containing all your methods so all work is done at once.
Now if I set Dog.prototype to another object it doesnt inherit from Animal anymore. So i searched and found that I should use Object.setPrototypeOf(obj, prototype)
to be able to achieve both
I think this exercise should also encourage using Object.setPrototypeOf
.
**Your code so far**
function Animal() { }
Animal.prototype = {
constructor: Animal,
eat: function() {
console.log("nom nom nom");
}
};
function Dog() { }
// Only change code below this line
Dog.prototype= {
go : function () {
console.log("go go go");
}
}
Object.setPrototypeOf(Dog.prototype, Animal.prototype);
let beagle = new Dog();
beagle.eat();
beagle.go();
**Your browser information:**
User Agent is: Mozilla/5.0
Challenge: Set the Child’s Prototype to an Instance of the Parent
Link to the challenge: