2018-10-10 18:03:03 -04:00
---
id: 587d7db0367417b2b2512b83
2021-02-06 04:42:36 +00:00
title: Use Inheritance So You Don't Repeat Yourself
2018-10-10 18:03:03 -04:00
challengeType: 1
2020-08-04 15:15:28 +08:00
forumTopicId: 301334
2021-01-13 03:31:00 +01:00
dashedName: use-inheritance-so-you-dont-repeat-yourself
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
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-12-16 00:37:30 -07:00
2021-02-06 04:42:36 +00:00
Notice in the example below that the `describe` method is shared by `Bird` and `Dog` :
2020-08-04 15:15:28 +08: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);
}
};
```
2021-02-06 04:42:36 +00: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` :
2020-08-04 15:15:28 +08:00
```js
function Animal() { };
Animal.prototype = {
constructor: Animal,
describe: function() {
console.log("My name is " + this.name);
}
};
```
2021-02-06 04:42:36 +00:00
Since `Animal` includes the `describe` method, you can remove it from `Bird` and `Dog` :
2020-08-04 15:15:28 +08:00
```js
Bird.prototype = {
constructor: Bird
};
Dog.prototype = {
constructor: Dog
};
```
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
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` .
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.prototype` should have the `eat` property.
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
```js
assert(Animal.prototype.hasOwnProperty('eat'));
2018-10-10 18:03:03 -04:00
```
2021-02-06 04:42:36 +00:00
`Bear.prototype` should not have the `eat` property.
2018-10-10 18:03:03 -04:00
```js
2020-12-16 00:37:30 -07:00
assert(!Bear.prototype.hasOwnProperty('eat'));
2018-10-10 18:03:03 -04:00
```
2021-02-06 04:42:36 +00:00
`Cat.prototype` should not have the `eat` property.
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(!Cat.prototype.hasOwnProperty('eat'));
2018-10-10 18:03:03 -04:00
```
2020-08-04 15:15:28 +08:00
2021-01-13 03:31:00 +01:00
# --seed--
## --seed-contents--
```js
function Cat(name) {
this.name = name;
}
Cat.prototype = {
constructor: Cat,
eat: function() {
console.log("nom nom nom");
}
};
function Bear(name) {
this.name = name;
}
Bear.prototype = {
constructor: Bear,
eat: function() {
console.log("nom nom nom");
}
};
function Animal() { }
Animal.prototype = {
constructor: Animal,
};
```
2020-12-16 00:37:30 -07:00
# --solutions--
2021-01-13 03:31:00 +01:00
```js
function Cat(name) {
this.name = name;
}
Cat.prototype = {
constructor: Cat
};
function Bear(name) {
this.name = name;
}
Bear.prototype = {
constructor: Bear
};
function Animal() { }
Animal.prototype = {
constructor: Animal,
eat: function() {
console.log("nom nom nom");
}
};
```