--- id: 587d7db2367417b2b2512b89 title: Use a Mixin to Add Common Behavior Between Unrelated Objects challengeType: 1 videoUrl: '' localeTitle: استخدم 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
```yml tests: - text: يجب أن تقوم التعليمات البرمجية بتعريف متغير 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.");' ```
## Challenge Seed
```js let bird = { name: "Donald", numLegs: 2 }; let boat = { name: "Warrior", type: "race-boat" }; // Add your code below this line ```
## Solution
```js // solution required ```