Don't Repeat Yourself (DRY). The reason repeated code is a problem is because any change requires fixing code in multiple places. This usually means more work for programmers and more room for errors.
Notice in the example below that the describe method is shared by Bird and Dog:
Bird.prototype = {The
constructor: Bird,
describe: function() {
console.log("My name is " + this.name);
}
};
Dog.prototype = {
constructor: Dog,
describe: function() {
console.log("My name is " + this.name);
}
};
describe method is repeated in two places. The code can be edited to follow the DRY principle by creating a supertype (or parent) called Animal:
function Animal() { };Since
Animal.prototype = {
constructor: Animal,
describe: function() {
console.log("My name is " + this.name);
}
};
Animal includes the describe method, you can remove it from Bird and Dog:
Bird.prototype = {
constructor: Bird
};
Dog.prototype = {
constructor: Dog
};
eat method is repeated in both Cat and Bear. Edit the code in the spirit of DRY by moving the eat method to the Animal supertype.
Animal.prototype should have the eat property.
testString: assert(Animal.prototype.hasOwnProperty('eat'), 'Animal.prototype should have the eat property.');
- text: Bear.prototype should not have the eat property.
testString: assert(!(Bear.prototype.hasOwnProperty('eat')), 'Bear.prototype should not have the eat property.');
- text: Cat.prototype should not have the eat property.
testString: assert(!(Cat.prototype.hasOwnProperty('eat')), 'Cat.prototype should not have the eat property.');
```