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 = {
منشئ: كلب
}؛
eat في كل من Cat and Bear . قم بتحرير الكود بروح DRY بتحريك طريقة eat إلى نوع supertype Animal . 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.");'
```