Skip to main content

--description--

In the previous challenge you saw the first step for inheriting behavior from the supertype (or parent) Animal: making a new instance of Animal.

This challenge covers the next step: set the prototype of the subtype (or child)—in this case, Bird—to be an instance of Animal.

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

Remember that the prototype is like the "recipe" for creating an object. In a way, the recipe for Bird now includes all the key "ingredients" from Animal.

let duck = new Bird("Donald");
duck.eat();

duck inherits all of Animal's properties, including the eat method.

--instructions--

Modify the code so that instances of Dog inherit from Animal.

--hints--

Dog.prototype should be an instance of Animal.

assert(Animal.prototype.isPrototypeOf(Dog.prototype));

--seed--

--seed-contents--

function Animal() { }

Animal.prototype = {
constructor: Animal,
eat: function() {
console.log("nom nom nom");
}
};

function Dog() { }

// Only change code below this line


let beagle = new Dog();

--solutions--

function Animal() { }

Animal.prototype = {
constructor: Animal,
eat: function() {
console.log("nom nom nom");
}
};

function Dog() { }
Dog.prototype = Object.create(Animal.prototype);

let beagle = new Dog();
beagle.eat();