Первый код:
function Animal() {}
Animal.prototype.eat = function() {
console.log("nom nom nom");
};
function Dog() {}
Dog.prototype = Object.create(Animal.prototype);
Dog.prototype = {
constructor: Dog,
bark: function() {
console.log("Woof!");
}
};
let beagle = new Dog();
console.clear();
beagle.eat(); // Should print "nom nom nom" but displays a error that eat
//is not a function.
beagle.bark();
Второй код:
function Animal() {}
Animal.prototype.eat = function() {
console.log("nom nom nom");
};
function Dog() {}
Dog.prototype = Object.create(Animal.prototype);
Dog.prototype.constructor = Dog;
Dog.prototype.bark = function() {
console.log("Woof!");
}
let beagle = new Dog();
console.clear();
beagle.eat(); // Prints "nom nom nom"
beagle.bark(); // Prints "Woof!"
Что не так с первым фрагментом кода, который beagle.eat()
не показывает правильный вывод.