freeCodeCamp/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/object-oriented-programming/use-a-mixin-to-add-common-behavior-between-unrelated-objects.chinese.md
Kristofer Koishigawa b3213fc892 fix(i18n): chinese test suite (#38220)
* fix: Chinese test suite

Add localeTiltes, descriptions, and adjust test text and testStrings to get the automated test suite working.

* fix: ran script, updated testStrings and solutions
2020-03-03 18:49:47 +05:30

2.7 KiB
Raw Blame History

id, title, challengeType, videoUrl, localeTitle
id title challengeType videoUrl localeTitle
587d7db2367417b2b2512b89 Use a Mixin to Add Common Behavior Between Unrelated Objects 1 使用Mixin在不相关的对象之间添加常见行为

Description

如您所见,行为通过继承来共享。但是,有些情况下继承不是最佳解决方案。对于像BirdAirplane这样的无关对象,继承不起作用。它们都可以飞行,但是Bird不是一种Airplane ,反之亦然。对于不相关的对象,最好使用mixinsmixin允许其他对象使用一组函数。
让flyMixin = functionobj{
obj.fly = function{
console.log“Flyingwooosh;
}
};
flyMixin接受任何对象并为其提供fly方法。
让bird = {
名称:“唐纳德”,
numLegs2
};

让plane = {
型号“777”
numPassengers524
};

flyMixin;
flyMixin平面;
这里将birdplane传递给flyMixin ,然后将fly函数分配给每个对象。现在birdplane都可以飞行:
bird.fly; //打印“飞行,嗖!”
plane.fly; //打印“飞行,嗖!”
注意mixin如何允许相同的fly方法被不相关的对象birdplane重用。

Instructions

创建一个名为glideMixinmixin ,它定义了一个名为glide的方法。然后使用glideMixinbirdboat都能够滑行。

Tests

tests:
  - text: 您的代码应该声明一个<code>glideMixin</code>变量,它是一个函数。
    testString: assert(typeof glideMixin === "function");
  - text: 你的代码应该使用<code>bird</code>对象上的<code>glideMixin</code>来为它提供<code>glide</code>方法。
    testString: assert(typeof bird.glide === "function");
  - text: 你的代码应该使用<code>boat</code>对象上的<code>glideMixin</code>来为它提供<code>glide</code>方法。
    testString: assert(typeof boat.glide === "function");

Challenge Seed

let bird = {
  name: "Donald",
  numLegs: 2
};

let boat = {
  name: "Warrior",
  type: "race-boat"
};

// Add your code below this line

Solution

// solution required