mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-05-01 10:01:04 -04:00
2.7 KiB
2.7 KiB
id, title, challengeType, forumTopicId, dashedName
| id | title | challengeType | forumTopicId | dashedName |
|---|---|---|---|---|
| 587d7db0367417b2b2512b83 | Use Inheritance So You Don't Repeat Yourself | 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 يتضمن method الـ describe ، يمكنك إزالته من Bird و Dog:
Bird.prototype = {
constructor: Bird
};
Dog.prototype = {
constructor: Dog
};
--instructions--
يتم تكرار method الـ eat في كل من Cat و Bear. قم بتعديل الكود بروح DRY عن طريق نقل method الـ eat إلى supertype الـ Animal.
--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");
}
};