2.6 KiB
id | title | challengeType | forumTopicId | dashedName |
---|---|---|---|---|
587d7db0367417b2b2512b83 | Usare l'ereditarietà per non ripeterti | 1 | 301334 | use-inheritance-so-you-dont-repeat-yourself |
--description--
C'è un principio nella programmazione chiamato Don't Repeat Yourself (DRY), che significa "non ripetere te stesso". Il motivo per cui il codice ripetuto è un problema è che qualsiasi modifica richiede di sistemare il codice in più punti. Questo di solito significa più lavoro per i programmatori e più spazio per gli errori.
Nota nell'esempio sottostante che il metodo describe
è condiviso da Bird
e da Dog
:
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);
}
};
Il metodo describe
viene ripetuto in due punti. Il codice può essere modificato per seguire il principio DRY creando un supertype
(o genitore) chiamato Animal
:
function Animal() { };
Animal.prototype = {
constructor: Animal,
describe: function() {
console.log("My name is " + this.name);
}
};
Dal momento che Animal
include il metodo describe
, puoi rimuoverlo da Bird
e Dog
:
Bird.prototype = {
constructor: Bird
};
Dog.prototype = {
constructor: Dog
};
--instructions--
Il metodo eat
viene ripetuto sia in Cat
che in Bear
. Modifica il codice nello spirito di DRY spostando il metodo eat
nel supertype
Animal
.
--hints--
Animal.prototype
dovrebbe avere la proprietà eat
.
assert(Animal.prototype.hasOwnProperty('eat'));
Bear.prototype
non dovrebbe avere la proprietà eat
.
assert(!Bear.prototype.hasOwnProperty('eat'));
Cat.prototype
non dovrebbe avere la proprietà eat
.
assert(!Cat.prototype.hasOwnProperty('eat'));
--seed--
--seed-contents--
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,
};
--solutions--
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");
}
};