--- id: 587d7db0367417b2b2512b83 title: Use Inheritance So You Don't Repeat Yourself challengeType: 1 videoUrl: '' localeTitle: Usa la herencia para que no te repitas --- ## Description
Hay un principio en la programación llamado Don't Repeat Yourself (DRY) . La razón por la cual el código repetido es un problema es porque cualquier cambio requiere un código de corrección en varios lugares. Esto generalmente significa más trabajo para los programadores y más espacio para errores. Observe en el siguiente ejemplo que Bird y Dog comparten el método de describe :
Bird.prototype = {
constructor: pájaro,
describe: function () {
console.log ("Mi nombre es" + this.name);
}
};

Dog.prototype = {
constructor: perro,
describe: function () {
console.log ("Mi nombre es" + this.name);
}
};
El método describe se repite en dos lugares. El código se puede editar para seguir el principio DRY creando un supertype (o padre) llamado Animal :
función Animal () {};

Animal.prototype = {
constructor: animal,
describe: function () {
console.log ("Mi nombre es" + this.name);
}
};
Ya que Animal incluye el método de describe , puedes eliminarlo de Bird and Dog :
Bird.prototype = {
constructor: pájaro
};

Dog.prototype = {
constructor: perro
};
## Instructions
El método de eat se repite tanto en el Cat como en el Bear . Edite el código en el espíritu de DRY moviendo el método de eat al supertype Animal .
## Tests
```yml tests: - text: Animal.prototype debe tener la propiedad eat . testString: 'assert(Animal.prototype.hasOwnProperty("eat"), "Animal.prototype should have the eat property.");' - text: Bear.prototype no debe tener la propiedad eat . testString: 'assert(!(Bear.prototype.hasOwnProperty("eat")), "Bear.prototype should not have the eat property.");' - text: Cat.prototype no debe tener la propiedad eat . testString: 'assert(!(Cat.prototype.hasOwnProperty("eat")), "Cat.prototype should not have the eat property.");' ```
## Challenge Seed
```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, }; ```
## Solution
```js // solution required ```