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
. mixin
المسمى glideMixin
الذي يحدد طريقة تسمى glide
. ثم استخدم glideMixin
لإعطاء كل من bird
boat
القدرة على الانزلاق. glideMixin
الذي يعد دالة.
testString: 'assert(typeof glideMixin === "function", "Your code should declare a glideMixin
variable that is a function.");'
- text: يجب أن تستخدم التعليمات البرمجية الخاصة بك glideMixin
على كائن bird
لإعطائه طريقة glide
.
testString: 'assert(typeof bird.glide === "function", "Your code should use the glideMixin
on the bird
object to give it the glide
method.");'
- text: يجب أن تستخدم التعليمات البرمجية الخاصة بك glideMixin
على كائن boat
لإعطائه طريقة glide
.
testString: 'assert(typeof boat.glide === "function", "Your code should use the glideMixin
on the boat
object to give it the glide
method.");'
```