--- id: 587d7db0367417b2b2512b83 title: Use Inheritance So You Don't Repeat Yourself challengeType: 1 videoUrl: '' localeTitle: استخدام الوراثة حتى لا تكرر نفسك --- ## Description
هناك مبدأ في البرمجة يسمى " Don't Repeat Yourself (DRY) . السبب في تكرار التعليمات البرمجية هو مشكلة لأن أي تغيير يتطلب إصلاح الكود في أماكن متعددة. هذا عادة ما يعني المزيد من العمل للمبرمجين ومجالاً أكبر للأخطاء. لاحظ في المثال أدناه أن طريقة describe تتم مشاركتها بواسطة Bird and Dog :
Bird.prototype = {
منشئ: الطيور ،
وصف: الوظيفة () {
console.log ("اسمي هو" + this.name)؛
}


Dog.prototype = {
منشئ: كلب ،
وصف: الوظيفة () {
console.log ("اسمي هو" + this.name)؛
}
تكرر طريقة describe في مكانين. يمكن تحرير الرمز ليتبع مبدأ DRY عن طريق إنشاء نوع supertype (أو الأصل) يسمى Animal :
وظيفة الحيوان () {}؛

Animal.prototype = {
منشئ: الحيوان ،
وصف: الوظيفة () {
console.log ("اسمي هو" + this.name)؛
}
بما أن Animal يشتمل على طريقة describe ، فيمكنك إزالتها من Bird and Dog :
Bird.prototype = {
منشئ: الطيور


Dog.prototype = {
منشئ: كلب
## Instructions
تتكرر طريقة eat في كل من Cat and Bear . قم بتحرير الكود بروح DRY بتحريك طريقة eat إلى نوع supertype Animal .
## Tests
```yml tests: - text: يجب أن يكون Animal.prototype خاصية eat . testString: 'assert(Animal.prototype.hasOwnProperty("eat"), "Animal.prototype should have the eat property.");' - text: يجب أن لا يكون Bear.prototype خاصية eat . testString: 'assert(!(Bear.prototype.hasOwnProperty("eat")), "Bear.prototype should not have the eat property.");' - text: يجب ألا يكون لدى Cat.prototype خاصية eat . testString: 'assert(!(Cat.prototype.hasOwnProperty("eat")), "Cat.prototype should not have the eat property.");' ```
## Challenge Seed
```js 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
```js // solution required ```