--- id: 587d7db0367417b2b2512b83 title: Use Inheritance So You Don't Repeat Yourself localeTitle: Usa la herencia para que no te repitas challengeType: 1 --- ## 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: Bird,
  describe: function() {
    console.log("My name is " + this.name);
  }
};

Dog.prototype = {
  constructor: Dog,
  describe: function() {
    console.log("My name is " + 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 :
function Animal() { };

Animal.prototype = {
  constructor: Animal,
  describe: function() {
    console.log("My name is " + this.name);
  }
};
Dado que Animal incluye el método de describe , puedes eliminarlo de Bird and Dog :
Bird.prototype = {
  constructor: Bird
};

Dog.prototype = {
  constructor: Dog
};
## Instructions
El método de eat se repite tanto en Cat como en 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 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"); } }; ```