3.9 KiB
3.9 KiB
id | title | challengeType | videoUrl | localeTitle |
---|---|---|---|---|
587d7db2367417b2b2512b89 | Use a Mixin to Add Common Behavior Between Unrelated Objects | 1 | استخدم Mixin لإضافة سلوك شائع بين الكائنات غير المرتبطة |
Description
Bird
و Airplane
. يستطيع كلاهما الطيران ، لكن Bird
ليست نوعًا من Airplane
والعكس صحيح. بالنسبة للكائنات غير المرتبطة ، من الأفضل استخدام mixins
. يسمح mixin
للكائنات الأخرى باستخدام مجموعة من الوظائف. let flyMixin = function (obj) {يأخذ
obj.fly = function () {
console.log ("الطائر ، wooosh!") ؛
}
}؛
flyMixin
أي كائن ويعطيه طريقة fly
. دع الطائر = {هنا يتم تمرير
الاسم: "دونالد" ،
numLegs: 2
}؛
واسمحوا الطائرة = {
model: "777" ،
num بالمرشحين: 524
}؛
flyMixin (الطيور)؛
flyMixin (طائرة)؛
bird
plane
في flyMixin
، والذي يقوم بتعيين وظيفة fly
لكل كائن. الآن يمكن bird
plane
الطيران: طائر يطير()؛ // prints "Flying، wooosh!"لاحظ كيف يسمح هذا
plane.fly ()؛ // prints "Flying، wooosh!"
mixin
بإعادة استخدام نفس طريقة fly
بواسطة كائنات لا علاقة لها bird
plane
. Instructions
mixin
المسمى glideMixin
الذي يحدد طريقة تسمى glide
. ثم استخدم glideMixin
لإعطاء كل من bird
boat
القدرة على الانزلاق. Tests
tests:
- text: يجب أن تقوم التعليمات البرمجية بتعريف متغير <code>glideMixin</code> الذي يعد دالة.
testString: 'assert(typeof glideMixin === "function", "Your code should declare a <code>glideMixin</code> variable that is a function.");'
- text: يجب أن تستخدم التعليمات البرمجية الخاصة بك <code>glideMixin</code> على كائن <code>bird</code> لإعطائه طريقة <code>glide</code> .
testString: 'assert(typeof bird.glide === "function", "Your code should use the <code>glideMixin</code> on the <code>bird</code> object to give it the <code>glide</code> method.");'
- text: يجب أن تستخدم التعليمات البرمجية الخاصة بك <code>glideMixin</code> على كائن <code>boat</code> لإعطائه طريقة <code>glide</code> .
testString: 'assert(typeof boat.glide === "function", "Your code should use the <code>glideMixin</code> on the <code>boat</code> object to give it the <code>glide</code> method.");'
Challenge Seed
let bird = {
name: "Donald",
numLegs: 2
};
let boat = {
name: "Warrior",
type: "race-boat"
};
// Add your code below this line
Solution
// solution required