3.1 KiB
id, title, challengeType, forumTopicId, dashedName
| id | title | challengeType | forumTopicId | dashedName |
|---|---|---|---|---|
| 587d7db0367417b2b2512b83 | Успадкування, щоб не повторюватися | 1 | 301334 | use-inheritance-so-you-dont-repeat-yourself |
--description--
У програмуванні існує принцип Don't Repeat Yourself (DRY), що в перекладі означає «Не повторюйся». Повторюваний код є проблемою через те, що будь-яка зміна вимагає виправлення коду в декількох місцях. Зазвичай це завдає більше роботи програмістам і призводить до більшої кількості помилок.
Зверніть увагу, що у прикладі метод describe розповсюджується на Bird та 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);
}
};
Метод describe повторюється у двох місцях. Код можна редагувати відповідно до принципу DRY, створивши supertype (або батьківський елемент) під назвою Animal:
function Animal() { };
Animal.prototype = {
constructor: Animal,
describe: function() {
console.log("My name is " + this.name);
}
};
Оскільки Animal містить метод describe, його можна видалити з Bird та Dog:
Bird.prototype = {
constructor: Bird
};
Dog.prototype = {
constructor: Dog
};
--instructions--
Метод eat повторюється як у Cat, так і Bear. Відредагуйте код за принципом DRY, перемістивши метод eat до Animal supertype.
--hints--
Animal.prototype повинен мати властивість eat.
assert(Animal.prototype.hasOwnProperty('eat'));
Bear.prototype не повинен мати властивість eat.
assert(!Bear.prototype.hasOwnProperty('eat'));
Cat.prototype не повинен мати властивість 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");
}
};