* Use dfn tags * remove misused <dfn> tags * Revert "remove misused <dfn> tags" This reverts commit b24968a96810f618d831410ac90a0bc452ebde50. * Update curriculum/challenges/english/01-responsive-web-design/basic-html-and-html5/fill-in-the-blank-with-placeholder-text.english.md Co-Authored-By: Randell Dawson <5313213+RandellDawson@users.noreply.github.com> * Make "array" lowercase Co-Authored-By: Randell Dawson <5313213+RandellDawson@users.noreply.github.com> * Fix dfn usage * Address last dfn tags
2.9 KiB
2.9 KiB
id, title, challengeType, forumTopicId
id | title | challengeType | forumTopicId |
---|---|---|---|
587d7db0367417b2b2512b83 | Use Inheritance So You Don't Repeat Yourself | 1 | 301334 |
Description
describe
method is shared by Bird
and 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);
}
};
The 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() { };
Animal.prototype = {
constructor: Animal,
describe: function() {
console.log("My name is " + this.name);
}
};
Since Animal
includes the describe
method, you can remove it from Bird
and Dog
:
Bird.prototype = {
constructor: Bird
};
Dog.prototype = {
constructor: Dog
};
Instructions
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
.
Tests
tests:
- text: <code>Animal.prototype</code> should have the <code>eat</code> property.
testString: assert(Animal.prototype.hasOwnProperty('eat'));
- text: <code>Bear.prototype</code> should not have the <code>eat</code> property.
testString: assert(!(Bear.prototype.hasOwnProperty('eat')));
- text: <code>Cat.prototype</code> should not have the <code>eat</code> property.
testString: assert(!(Cat.prototype.hasOwnProperty('eat')));
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
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");
}
};