2018-10-10 18:03:03 -04:00
---
id: 587d7db1367417b2b2512b87
2021-02-06 04:42:36 +00:00
title: Add Methods After Inheritance
2018-10-10 18:03:03 -04:00
challengeType: 1
2020-08-04 15:15:28 +08:00
forumTopicId: 301315
2021-01-13 03:31:00 +01:00
dashedName: add-methods-after-inheritance
2018-10-10 18:03:03 -04:00
---
2020-12-16 00:37:30 -07:00
# --description--
2021-02-06 04:42:36 +00:00
A constructor function that inherits its `prototype` object from a supertype constructor function can still have its own methods in addition to inherited methods.
2020-12-16 00:37:30 -07:00
2021-02-06 04:42:36 +00:00
For example, `Bird` is a constructor that inherits its `prototype` from `Animal` :
2020-08-04 15:15:28 +08: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;
```
2021-02-06 04:42:36 +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:
2020-08-04 15:15:28 +08:00
```js
Bird.prototype.fly = function() {
console.log("I'm flying!");
};
```
2021-02-06 04:42:36 +00:00
Now instances of `Bird` will have both `eat()` and `fly()` methods:
2020-08-04 15:15:28 +08:00
```js
let duck = new Bird();
duck.eat(); // prints "nom nom nom"
duck.fly(); // prints "I'm flying!"
```
2020-12-16 00:37:30 -07:00
# --instructions--
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +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.
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
# --hints--
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
`Animal` should not respond to the `bark()` method.
2018-10-10 18:03:03 -04:00
```js
2020-12-16 00:37:30 -07:00
assert(typeof Animal.prototype.bark == 'undefined');
2018-10-10 18:03:03 -04:00
```
2021-02-06 04:42:36 +00:00
`Dog` should inherit the `eat()` method from `Animal` .
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
```js
assert(typeof Dog.prototype.eat == 'function');
```
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
`Dog` should have the `bark()` method as an `own` property.
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
```js
assert(Dog.prototype.hasOwnProperty('bark'));
```
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
`beagle` should be an `instanceof` `Animal` .
2020-08-04 15:15:28 +08:00
2018-10-10 18:03:03 -04:00
```js
2020-12-16 00:37:30 -07:00
assert(beagle instanceof Animal);
```
2020-08-04 15:15:28 +08:00
2021-02-06 04:42:36 +00:00
The constructor for `beagle` should be set to `Dog` .
2020-08-04 15:15:28 +08:00
2020-12-16 00:37:30 -07:00
```js
assert(beagle.constructor === Dog);
2018-10-10 18:03:03 -04:00
```
2020-08-04 15:15:28 +08:00
2021-02-06 04:42:36 +00:00
`beagle.eat()` should log `"nom nom nom"`
```js
console.log = function (msg) {
throw msg;
};
assert.throws(() => beagle.eat(), 'nom nom nom');
```
`beagle.bark()` should log `"Woof!"`
```js
console.log = function (msg) {
throw msg;
};
assert.throws(() => beagle.bark(), 'Woof!');
```
2021-01-13 03:31:00 +01:00
# --seed--
## --seed-contents--
```js
function Animal() { }
Animal.prototype.eat = function() { console.log("nom nom nom"); };
function Dog() { }
// Only change code below this line
// Only change code above this line
let beagle = new Dog();
```
2020-12-16 00:37:30 -07:00
# --solutions--
2021-01-13 03:31:00 +01: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();
```