freeCodeCamp/curriculum/challenges/spanish/02-javascript-algorithms-an.../object-oriented-programming/use-inheritance-so-you-dont...

81 lines
3.2 KiB
Markdown
Raw Normal View History

2018-10-08 17:34:43 +00:00
---
id: 587d7db0367417b2b2512b83
title: Use Inheritance So You Don't Repeat Yourself
challengeType: 1
2018-10-10 20:20:40 +00:00
videoUrl: ''
localeTitle: Usa la herencia para que no te repitas
2018-10-08 17:34:43 +00:00
---
## Description
2018-10-10 20:20:40 +00:00
<section id="description"> Hay un principio en la programación llamado <code>Don&#39;t Repeat Yourself (DRY)</code> . 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 <code>Bird</code> y <code>Dog</code> comparten el método de <code>describe</code> : <blockquote> Bird.prototype = { <br> constructor: pájaro, <br> describe: function () { <br> console.log (&quot;Mi nombre es&quot; + this.name); <br> } <br> }; <br><br> Dog.prototype = { <br> constructor: perro, <br> describe: function () { <br> console.log (&quot;Mi nombre es&quot; + this.name); <br> } <br> }; </blockquote> El método <code>describe</code> se repite en dos lugares. El código se puede editar para seguir el principio <code>DRY</code> creando un <code>supertype</code> (o padre) llamado <code>Animal</code> : <blockquote> función Animal () {}; <br><br> Animal.prototype = { <br> constructor: animal, <br> describe: function () { <br> console.log (&quot;Mi nombre es&quot; + this.name); <br> } <br> }; </blockquote> Ya que <code>Animal</code> incluye el método de <code>describe</code> , puedes eliminarlo de <code>Bird</code> and <code>Dog</code> : <blockquote> Bird.prototype = { <br> constructor: pájaro <br> }; <br><br> Dog.prototype = { <br> constructor: perro <br> }; </blockquote></section>
2018-10-08 17:34:43 +00:00
## Instructions
2018-10-10 20:20:40 +00:00
<section id="instructions"> El método de <code>eat</code> se repite tanto en el <code>Cat</code> como en el <code>Bear</code> . Edite el código en el espíritu de <code>DRY</code> moviendo el método de <code>eat</code> al <code>supertype</code> <code>Animal</code> . </section>
2018-10-08 17:34:43 +00:00
## Tests
<section id='tests'>
```yml
tests:
- text: <code>Animal.prototype</code> debe tener la propiedad <code>eat</code> .
testString: 'assert(Animal.prototype.hasOwnProperty("eat"), "<code>Animal.prototype</code> should have the <code>eat</code> property.");'
- text: <code>Bear.prototype</code> no debe tener la propiedad <code>eat</code> .
testString: 'assert(!(Bear.prototype.hasOwnProperty("eat")), "<code>Bear.prototype</code> should not have the <code>eat</code> property.");'
- text: <code>Cat.prototype</code> no debe tener la propiedad <code>eat</code> .
testString: 'assert(!(Cat.prototype.hasOwnProperty("eat")), "<code>Cat.prototype</code> should not have the <code>eat</code> property.");'
```
</section>
## Challenge Seed
<section id='challengeSeed'>
<div id='js-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,
};
2018-10-10 20:20:40 +00:00
2018-10-08 17:34:43 +00:00
```
</div>
</section>
## Solution
<section id='solution'>
```js
2018-10-10 20:20:40 +00:00
// solution required
2018-10-08 17:34:43 +00:00
```
</section>