freeCodeCamp/curriculum/challenges/arabic/02-javascript-algorithms-an.../object-oriented-programming/use-a-mixin-to-add-common-b...

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