function Animal(name) { this.name = name; } Animal.prototype.speak = function() { console.log(this.name + ' makes a noise.'); } function Dog(name) { Animal.call(this, name); } const dog = new Dog('Rex'); dog.speak();
In order to inherit the prototype methods from Animal, we need to use Object.create to create a new object that inherits from Animal.prototype and assign it to Dog.prototype.
function Animal(name) { this.name = name; } Animal.prototype.speak = function() { console.log(this.name + ' makes a noise.'); } function Dog(name) { Animal.call(this, name); } Dog.prototype = Object.create(Animal.prototype); const dog = new Dog('Rex'); dog.speak();