2018-09-30 23:01:58 +01:00
---
id: 587d7db1367417b2b2512b87
title: Add Methods After Inheritance
challengeType: 1
2019-08-05 09:17:33 -07:00
forumTopicId: 301315
2018-09-30 23:01:58 +01:00
---
## Description
< section id = 'description' >
2019-10-27 15:45:37 -01:00
A constructor function that inherits its < code > prototype< / code > object from a supertype constructor function can still have its own methods in addition to inherited methods.
2018-09-30 23:01:58 +01:00
For example, < code > Bird< / code > is a constructor that inherits its < code > prototype< / code > from < code > Animal< / code > :
2019-05-17 06:20:30 -07:00
```js
function Animal() { }
Animal.prototype.eat = function() {
console.log("nom nom nom");
};
function Bird() { }
Bird.prototype = Object.create(Animal.prototype);
Bird.prototype.constructor = Bird;
```
2018-09-30 23:01:58 +01:00
In addition to what is inherited from < code > Animal< / code > , you want to add behavior that is unique to < code > Bird< / code > objects. Here, < code > Bird< / code > will get a < code > fly()< / code > function. Functions are added to < code > Bird's< / code > < code > prototype< / code > the same way as any constructor function:
2019-05-17 06:20:30 -07:00
```js
Bird.prototype.fly = function() {
console.log("I'm flying!");
};
```
2018-09-30 23:01:58 +01:00
Now instances of < code > Bird< / code > will have both < code > eat()< / code > and < code > fly()< / code > methods:
2019-05-17 06:20:30 -07:00
```js
let duck = new Bird();
duck.eat(); // prints "nom nom nom"
duck.fly(); // prints "I'm flying!"
```
2018-09-30 23:01:58 +01:00
< / section >
## Instructions
< section id = 'instructions' >
Add all necessary code so the < code > Dog< / code > object inherits from < code > Animal< / code > and the < code > Dog's< / code > < code > prototype< / code > constructor is set to Dog. Then add a < code > bark()< / code > method to the < code > Dog< / code > object so that < code > beagle< / code > can both < code > eat()< / code > and < code > bark()< / code > . The < code > bark()< / code > method should print "Woof!" to the console.
< / section >
## Tests
< section id = 'tests' >
```yml
2018-10-04 14:37:37 +01:00
tests:
- text: < code > Animal</ code > should not respond to the < code > bark()</ code > method.
2019-07-24 02:32:04 -07:00
testString: assert(typeof Animal.prototype.bark == "undefined");
2018-10-04 14:37:37 +01:00
- text: < code > Dog</ code > should inherit the < code > eat()</ code > method from < code > Animal</ code > .
2019-07-24 02:32:04 -07:00
testString: assert(typeof Dog.prototype.eat == "function");
2018-10-04 14:37:37 +01:00
- text: < code > Dog</ code > should have the < code > bark()</ code > method as an < code > own</ code > property.
2019-07-24 02:32:04 -07:00
testString: assert(Dog.prototype.hasOwnProperty('bark'));
2018-10-04 14:37:37 +01:00
- text: < code > beagle</ code > should be an < code > instanceof</ code > < code > Animal</ code > .
2019-07-24 02:32:04 -07:00
testString: assert(beagle instanceof Animal);
2018-10-04 14:37:37 +01:00
- text: The constructor for < code > beagle</ code > should be set to < code > Dog</ code > .
2019-07-24 02:32:04 -07:00
testString: assert(beagle.constructor === Dog);
2018-09-30 23:01:58 +01:00
```
< / section >
## Challenge Seed
< section id = 'challengeSeed' >
< div id = 'js-seed' >
```js
function Animal() { }
Animal.prototype.eat = function() { console.log("nom nom nom"); };
function Dog() { }
2020-03-08 07:46:28 -07:00
// Only change code below this line
2018-09-30 23:01:58 +01:00
2020-03-08 07:46:28 -07:00
// Only change code above this line
2018-09-30 23:01:58 +01:00
let beagle = new Dog();
```
< / div >
< / section >
## Solution
< section id = 'solution' >
```js
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();
beagle.eat();
beagle.bark();
```
< / section >