--- id: 587d7db1367417b2b2512b88 title: Override Inherited Methods challengeType: 1 videoUrl: '' localeTitle: تجاوز الأساليب الموروثة --- ## Description
في الدروس السابقة ، تعلمت أن الكائن يمكن أن يرث سلوكه (أساليب) من كائن آخر عن طريق استنساخ كائن prototype الخاص به:
ChildObject.prototype = Object.create (ParentObject.prototype)؛
ثم تلقت ChildObject الخاصة عن طريق ChildObject prototype :
ChildObject.prototype.methodName = function () {...}؛
من الممكن تجاوز طريقة وراثية. ويتم ذلك بنفس الطريقة - عن طريق إضافة طريقة إلى ChildObject.prototype باستخدام نفس اسم الأسلوب الذي يتم تجاوزه. في ما يلي مثال Bird يتفوق على طريقة eat() الموروثة من Animal :
وظيفة الحيوان () {}
Animal.prototype.eat = function () {
return "nom nom nom"؛

وظيفة الطيور () {}

// وراثة جميع الطرق من الحيوان
Bird.prototype = Object.create (Animal.prototype)؛

// Bird.eat () يتخطى Animal.eat ()
Bird.prototype.eat = function () {
عودة "بيك بيك بيك"؛
إذا كان لديك مثيل ، let duck = new Bird(); واستدعاء duck.eat() ، هذه هي الطريقة التي يبدو جافا سكريبت لأسلوب على duck's prototype سلسلة: 1. بطة => هل تأكل () المعرفة هنا؟ رقم 2. الطيور => هل تناول الطعام () المعرفة هنا؟ => نعم. تنفيذ ذلك ووقف البحث. 3. يتم تعريف Animal => eat () أيضًا ، لكن JavaScript توقفت عن البحث قبل الوصول إلى هذا المستوى. 4. Object => توقف JavaScript عن البحث قبل الوصول إلى هذا المستوى.
## Instructions
تجاوز طريقة fly() لـ Penguin بحيث تُرجع "Alas ، هذا طائر بلا fly() ".
## Tests
```yml tests: - text: يجب أن ترجع penguin.fly() السلسلة "للأسف ، هذا طائر بلا طيار". testString: 'assert(penguin.fly() === "Alas, this is a flightless bird.", "penguin.fly() should return the string "Alas, this is a flightless bird."");' - text: يجب أن ترجع طريقة bird.fly() "أنا bird.fly() !" testString: 'assert((new Bird()).fly() === "I am flying!", "The bird.fly() method should return "I am flying!"");' ```
## Challenge Seed
```js function Bird() { } Bird.prototype.fly = function() { return "I am flying!"; }; function Penguin() { } Penguin.prototype = Object.create(Bird.prototype); Penguin.prototype.constructor = Penguin; // Add your code below this line // Add your code above this line let penguin = new Penguin(); console.log(penguin.fly()); ```
## Solution
```js // solution required ```