3.2 KiB
3.2 KiB
id, title, challengeType, videoUrl, localeTitle
| id | title | challengeType | videoUrl | localeTitle |
|---|---|---|---|---|
| 587d7db0367417b2b2512b83 | Use Inheritance So You Don't Repeat Yourself | 1 | Usa la herencia para que no te repitas |
Description
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 = {El método
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);
}
};
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 () {};Ya que
Animal.prototype = {
constructor: animal,
describe: function () {
console.log ("Mi nombre es" + this.name);
}
};
Animal incluye el método de describe , puedes eliminarlo de Bird and Dog : Bird.prototype = {
constructor: pájaro
};
Dog.prototype = {
constructor: perro
};
Instructions
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
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.");'
Challenge Seed
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
// solution required