Files

41 lines
637 B
Markdown
Raw Normal View History

2018-10-12 15:37:13 -04:00
---
title: Use Inheritance So You Don't Repeat Yourself
---
# Use Inheritance So You Don't Repeat Yourself
2018-10-12 15:37:13 -04:00
---
## Solutions
<details><summary>Solution 1 (Click to Show/Hide)</summary>
2018-10-12 15:37:13 -04:00
```javascript
function Cat(name) {
this.name = name;
}
2018-10-12 15:37:13 -04:00
Cat.prototype = {
constructor: Cat
};
function Bear(name) {
this.name = name;
}
2018-10-12 15:37:13 -04:00
Bear.prototype = {
constructor: Bear
};
function Animal() {}
2018-10-12 15:37:13 -04:00
Animal.prototype = {
constructor: Animal,
eat: function() {
console.log("nom nom nom");
}
};
```
#### Code Explanation
* Remove the "eat" method from Cat.prototype and Bear.prototype and add it to the Animal.prototype.
</details>