4.0 KiB
4.0 KiB
id, title, challengeType, videoUrl, localeTitle
| id | title | challengeType | videoUrl | localeTitle |
|---|---|---|---|---|
| 587d7db1367417b2b2512b87 | Add Methods After Inheritance | 1 | إضافة طرق بعد الوراثة |
Description
prototype من دالة منشئ supertype قادرة على استخدام أساليبها الخاصة بالإضافة إلى الأساليب الموروثة. على سبيل المثال ، Bird هو مُنشئ يرث prototype من Animal : وظيفة الحيوان () {}بالإضافة إلى ما هو موروث من
Animal.prototype.eat = function () {
console.log ("nom nom nom")؛
}؛
وظيفة الطيور () {}
Bird.prototype = Object.create (Animal.prototype)؛
Bird.prototype.constructor = Bird؛
Animal ، فأنت تريد إضافة سلوك فريد لكائنات Bird . هنا ، سوف تحصل Bird على وظيفة fly() . يتم إضافة وظائف إلى Bird's prototype بنفس الطريقة مثل أي وظيفة المنشئ: Bird.prototype.fly = function () {الآن سوف يكون لديك
console.log ("أنا أطير!") ؛
}؛
Bird من Bird على كلا أساليب eat() و fly() : السماح بطة = الطيور الجديدة () ؛
duck.eat ()؛ // prints "nom nom nom"
duck.fly ()؛ // prints "أنا أطير!"
Instructions
Dog يرث الكائن من Animal و Dog's prototype يتم تعيين المنشئ إلى الكلب. ثم أضف طريقة bark() إلى جسم Dog حتى يستطيع beagle أن eat() bark() . يجب أن تطبع طريقة bark() "Woof!" إلى وحدة التحكم. Tests
tests:
- text: لا ينبغي أن يستجيب <code>Animal</code> لطريقة <code>bark()</code> .
testString: 'assert(typeof Animal.prototype.bark == "undefined", "<code>Animal</code> should not respond to the <code>bark()</code> method.");'
- text: يجب أن يرث <code>Dog</code> طريقة <code>eat()</code> من <code>Animal</code> .
testString: 'assert(typeof Dog.prototype.eat == "function", "<code>Dog</code> should inherit the <code>eat()</code> method from <code>Animal</code>.");'
- text: يجب أن يكون <code>Dog</code> أسلوب <code>bark()</code> كخاصية <code>own</code> .
testString: 'assert(Dog.prototype.hasOwnProperty("bark"), "<code>Dog</code> should have the <code>bark()</code> method as an <code>own</code> property.");'
- text: <code>beagle</code> يجب أن يكون <code>instanceof</code> <code>Animal</code> .
testString: 'assert(beagle instanceof Animal, "<code>beagle</code> should be an <code>instanceof</code> <code>Animal</code>.");'
- text: يجب تعيين منشئ <code>beagle</code> <code>Dog</code> .
testString: 'assert(beagle.constructor === Dog, "The constructor for <code>beagle</code> should be set to <code>Dog</code>.");'
Challenge Seed
function Animal() { }
Animal.prototype.eat = function() { console.log("nom nom nom"); };
function Dog() { }
// Add your code below this line
// Add your code above this line
let beagle = new Dog();
beagle.eat(); // Should print "nom nom nom"
beagle.bark(); // Should print "Woof!"
Solution
// solution required