3.6 KiB
3.6 KiB
id | title | challengeType | videoUrl | localeTitle |
---|---|---|---|---|
587d7db1367417b2b2512b88 | Override Inherited Methods | 1 | تجاوز الأساليب الموروثة |
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
tests:
- text: يجب أن ترجع <code>penguin.fly()</code> السلسلة "للأسف ، هذا طائر بلا طيار".
testString: 'assert(penguin.fly() === "Alas, this is a flightless bird.", "<code>penguin.fly()</code> should return the string "Alas, this is a flightless bird."");'
- text: يجب أن ترجع طريقة <code>bird.fly()</code> "أنا <code>bird.fly()</code> !"
testString: 'assert((new Bird()).fly() === "I am flying!", "The <code>bird.fly()</code> method should return "I am flying!"");'
Challenge Seed
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
// solution required