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
2018-09-30 23:01:58 +01:00
---
## Description
< section id = '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.
2018-09-30 23:01:58 +01:00
Notice in the example below that the < code > describe< / code > method is shared by < code > Bird< / code > and < code > Dog< / code > :
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);
}
};
```
2019-10-27 15:45:37 -01:00
The < code > describe< / code > method is repeated in two places. The code can be edited to follow the DRY principle by creating a < code > supertype< / code > (or parent) called < code > Animal< / code > :
2019-05-17 06:20:30 -07:00
```js
function Animal() { };
Animal.prototype = {
constructor: Animal,
describe: function() {
console.log("My name is " + this.name);
}
};
```
2018-09-30 23:01:58 +01:00
Since < code > Animal< / code > includes the < code > describe< / code > method, you can remove it from < code > Bird< / code > and < code > Dog< / code > :
2019-05-17 06:20:30 -07:00
```js
Bird.prototype = {
constructor: Bird
};
Dog.prototype = {
constructor: Dog
};
```
2018-09-30 23:01:58 +01:00
< / section >
## Instructions
< section id = 'instructions' >
2019-10-27 15:45:37 -01:00
The < code > eat< / code > method is repeated in both < code > Cat< / code > and < code > Bear< / code > . Edit the code in the spirit of DRY by moving the < code > eat< / code > method to the < code > Animal< / code > < code > supertype< / code > .
2018-09-30 23:01:58 +01:00
< / section >
## Tests
< section id = 'tests' >
```yml
2018-10-04 14:37:37 +01:00
tests:
- text: < code > Animal.prototype</ code > should have the < code > eat</ code > property.
2019-07-24 02:32:04 -07:00
testString: assert(Animal.prototype.hasOwnProperty('eat'));
2018-10-04 14:37:37 +01:00
- text: < code > Bear.prototype</ code > should not have the < code > eat</ code > property.
2019-07-24 02:32:04 -07:00
testString: assert(!(Bear.prototype.hasOwnProperty('eat')));
2018-10-04 14:37:37 +01:00
- text: < code > Cat.prototype</ code > should not have the < code > eat</ code > property.
2019-07-24 02:32:04 -07:00
testString: assert(!(Cat.prototype.hasOwnProperty('eat')));
2018-09-30 23:01:58 +01:00
```
< / section >
## Challenge Seed
< section id = 'challengeSeed' >
< div id = 'js-seed' >
```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
};
```
< / div >
< / section >
## Solution
< section id = 'solution' >
```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");
}
};
```
< / section >