I’m trying to complete this challenge by creating a Dog.prototype object and I don’t understand why it’s not working.
The code I tried was
Dog.prototype = Object.create(Animal.prototype);
Dog.prototype = {
constructor: Dog,
bark: function() {
console.log("Woof!");
}
};
but when I do this, the error messages I get are:
- Dog should inherit the eat() method from Animal.
- beagle should be an instanceof Animal.
- beagle.eat() should log “nom nom nom”
It seems to only work when I add each property to the prototype individually, like:
Dog.prototype = Object.create(Animal.prototype);
Dog.prototype.constructor = Dog;
Dog.prototype.bark = function() { console.log("Woof!") };
Why is this? And if I’m doing something wrong with my prototype object, how would I correctly complete this challenge by creating an object prototype rather than adding the properties individually? Thank you!