freeCodeCamp/curriculum/challenges/arabic/02-javascript-algorithms-an.../object-oriented-programming/use-inheritance-so-you-dont...

3.5 KiB

id title challengeType videoUrl localeTitle
587d7db0367417b2b2512b83 Use Inheritance So You Don't Repeat Yourself 1 استخدام الوراثة حتى لا تكرر نفسك

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

tests:
  - text: يجب أن يكون <code>Animal.prototype</code> خاصية <code>eat</code> .
    testString: 'assert(Animal.prototype.hasOwnProperty("eat"), "<code>Animal.prototype</code> should have the <code>eat</code> property.");'
  - text: يجب أن لا يكون <code>Bear.prototype</code> خاصية <code>eat</code> .
    testString: 'assert(!(Bear.prototype.hasOwnProperty("eat")), "<code>Bear.prototype</code> should not have the <code>eat</code> property.");'
  - text: يجب ألا يكون لدى <code>Cat.prototype</code> خاصية <code>eat</code> .
    testString: 'assert(!(Cat.prototype.hasOwnProperty("eat")), "<code>Cat.prototype</code> should not have the <code>eat</code> property.");'

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

// solution required