2018-09-30 23:01:58 +01:00
---
id: 587d7db0367417b2b2512b83
title: Use Inheritance So You Don't Repeat Yourself
challengeType: 1
2019-08-05 09:17:33 -07:00
forumTopicId: 301334
2021-01-13 03:31:00 +01:00
dashedName: use-inheritance-so-you-dont-repeat-yourself
2018-09-30 23:01:58 +01:00
---
2020-11-27 19:02:05 +01:00
# --description--
2019-10-27 15:45:37 -01:00
There's a principle in programming called < dfn > Don't Repeat Yourself (DRY)< / dfn > . The reason repeated code is a problem is because any change requires fixing code in multiple places. This usually means more work for programmers and more room for errors.
2020-11-27 19:02:05 +01:00
Notice in the example below that the `describe` method is shared by `Bird` and `Dog` :
2019-05-17 06:20:30 -07:00
```js
Bird.prototype = {
constructor: Bird,
describe: function() {
console.log("My name is " + this.name);
}
};
Dog.prototype = {
constructor: Dog,
describe: function() {
console.log("My name is " + this.name);
}
};
```
2020-11-27 19:02:05 +01:00
The `describe` method is repeated in two places. The code can be edited to follow the DRY principle by creating a `supertype` (or parent) called `Animal` :
2019-05-17 06:20:30 -07:00
```js
function Animal() { };
Animal.prototype = {
constructor: Animal,
describe: function() {
console.log("My name is " + this.name);
}
};
```
2020-11-27 19:02:05 +01:00
Since `Animal` includes the `describe` method, you can remove it from `Bird` and `Dog` :
2019-05-17 06:20:30 -07:00
```js
Bird.prototype = {
constructor: Bird
};
Dog.prototype = {
constructor: Dog
};
```
2020-11-27 19:02:05 +01:00
# --instructions--
The `eat` method is repeated in both `Cat` and `Bear` . Edit the code in the spirit of DRY by moving the `eat` method to the `Animal` `supertype` .
# --hints--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
`Animal.prototype` should have the `eat` property.
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
assert(Animal.prototype.hasOwnProperty('eat'));
```
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
`Bear.prototype` should not have the `eat` property.
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
assert(!Bear.prototype.hasOwnProperty('eat'));
2018-09-30 23:01:58 +01:00
```
2020-11-27 19:02:05 +01:00
`Cat.prototype` should not have the `eat` property.
```js
assert(!Cat.prototype.hasOwnProperty('eat'));
```
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
# --seed--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
## --seed-contents--
2018-09-30 23:01:58 +01:00
```js
function Cat(name) {
2018-10-08 01:01:53 +01:00
this.name = name;
2018-09-30 23:01:58 +01:00
}
Cat.prototype = {
2018-10-08 01:01:53 +01:00
constructor: Cat,
2018-09-30 23:01:58 +01:00
eat: function() {
console.log("nom nom nom");
}
};
function Bear(name) {
2018-10-08 01:01:53 +01:00
this.name = name;
2018-09-30 23:01:58 +01:00
}
Bear.prototype = {
2018-10-08 01:01:53 +01:00
constructor: Bear,
2018-09-30 23:01:58 +01:00
eat: function() {
console.log("nom nom nom");
}
};
function Animal() { }
Animal.prototype = {
constructor: Animal,
2018-10-08 01:01:53 +01:00
2018-09-30 23:01:58 +01:00
};
```
2020-11-27 19:02:05 +01:00
# --solutions--
2018-09-30 23:01:58 +01:00
```js
function Cat(name) {
2018-10-08 01:01:53 +01:00
this.name = name;
2018-09-30 23:01:58 +01:00
}
Cat.prototype = {
constructor: Cat
};
function Bear(name) {
2018-10-08 01:01:53 +01:00
this.name = name;
2018-09-30 23:01:58 +01:00
}
Bear.prototype = {
constructor: Bear
};
function Animal() { }
Animal.prototype = {
constructor: Animal,
eat: function() {
console.log("nom nom nom");
}
};
```