2018-09-30 22:01:58 +00:00
---
id: 587d7db1367417b2b2512b87
title: Add Methods After Inheritance
challengeType: 1
2019-08-05 16:17:33 +00:00
forumTopicId: 301315
2021-01-13 02:31:00 +00:00
dashedName: add-methods-after-inheritance
2018-09-30 22:01:58 +00:00
---
2020-11-27 18:02:05 +00:00
# --description--
A constructor function that inherits its `prototype` object from a supertype constructor function can still have its own methods in addition to inherited methods.
For example, `Bird` is a constructor that inherits its `prototype` from `Animal` :
2019-05-17 13:20:30 +00: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;
```
2020-11-27 18:02:05 +00:00
In addition to what is inherited from `Animal` , you want to add behavior that is unique to `Bird` objects. Here, `Bird` will get a `fly()` function. Functions are added to `Bird's` `prototype` the same way as any constructor function:
2019-05-17 13:20:30 +00:00
```js
Bird.prototype.fly = function() {
console.log("I'm flying!");
};
```
2020-11-27 18:02:05 +00:00
Now instances of `Bird` will have both `eat()` and `fly()` methods:
2019-05-17 13:20:30 +00:00
```js
let duck = new Bird();
2021-03-03 00:12:12 +00:00
duck.eat();
duck.fly();
2019-05-17 13:20:30 +00:00
```
2021-03-03 00:12:12 +00:00
`duck.eat()` would display the string `nom nom nom` in the console, and `duck.fly()` would display the string `I'm flying!` .
2020-11-27 18:02:05 +00:00
# --instructions--
2021-03-03 00:12:12 +00:00
Add all necessary code so the `Dog` object inherits from `Animal` and the `Dog` 's `prototype` constructor is set to `Dog` . Then add a `bark()` method to the `Dog` object so that `beagle` can both `eat()` and `bark()` . The `bark()` method should print `Woof!` to the console.
2020-11-27 18:02:05 +00:00
# --hints--
`Animal` should not respond to the `bark()` method.
```js
assert(typeof Animal.prototype.bark == 'undefined');
```
`Dog` should inherit the `eat()` method from `Animal` .
```js
assert(typeof Dog.prototype.eat == 'function');
```
`Dog` should have the `bark()` method as an `own` property.
```js
assert(Dog.prototype.hasOwnProperty('bark'));
```
`beagle` should be an `instanceof` `Animal` .
```js
assert(beagle instanceof Animal);
```
The constructor for `beagle` should be set to `Dog` .
```js
assert(beagle.constructor === Dog);
2018-09-30 22:01:58 +00:00
```
2021-03-03 00:12:12 +00:00
`beagle.eat()` should log the string `nom nom nom`
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
```js
console.log = function (msg) {
throw msg;
};
assert.throws(() => beagle.eat(), 'nom nom nom');
```
2018-09-30 22:01:58 +00:00
2021-03-03 00:12:12 +00:00
`beagle.bark()` should log the string `Woof!`
2020-11-27 18:02:05 +00:00
```js
console.log = function (msg) {
throw msg;
};
assert.throws(() => beagle.bark(), 'Woof!');
```
# --seed--
## --seed-contents--
2018-09-30 22:01:58 +00:00
```js
function Animal() { }
Animal.prototype.eat = function() { console.log("nom nom nom"); };
function Dog() { }
2020-03-08 14:46:28 +00:00
// Only change code below this line
2018-09-30 22:01:58 +00:00
2020-03-08 14:46:28 +00:00
// Only change code above this line
2018-09-30 22:01:58 +00:00
let beagle = new Dog();
```
2020-11-27 18:02:05 +00:00
# --solutions--
2018-09-30 22:01:58 +00:00
```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();
```