Files
Nicholas Carrigan (he/him) 3da4be21bb chore: seed chinese traditional (#42005)
Seeds the chinese traditional files manually so we can deploy to
staging.
2021-05-05 22:43:49 +05:30

2.3 KiB
Raw Permalink Blame History

id, title, challengeType, forumTopicId, dashedName
id title challengeType forumTopicId dashedName
587d7db2367417b2b2512b89 使用 Mixin 在不相關對象之間添加共同行爲 1 301331 use-a-mixin-to-add-common-behavior-between-unrelated-objects

--description--

正如你所見,行爲是可以通過繼承來共享的。 然而,在有些情況下,繼承不是最好的解決方案。 繼承不適用於不相關的對象,比如 BirdAirplane。 雖然它們都可以飛行,但是 Bird 並不是一種 Airplane,反之亦然。

對於不相關的對象,更好的方法是使用 mixins。 mixin 允許其他對象使用函數集合。

let flyMixin = function(obj) {
  obj.fly = function() {
    console.log("Flying, wooosh!");
  }
};

flyMixin 能接受任何對象,併爲其提供 fly 方法。

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

let plane = {
  model: "777",
  numPassengers: 524
};

flyMixin(bird);
flyMixin(plane);

這裏的 flyMixin 接收了birdplane 對象,然後將 fly 方法分配給了每一個對象。 現在 birdplane 都可以飛行了:

bird.fly();
plane.fly();

控制檯將顯示字符串 Flying, wooosh! 兩次,每 .fly() 調用都會顯示。

注意觀察 mixin 是如何允許相同的 fly 方法被不相關的對象 birdplane 重用的。

--instructions--

創建一個名爲 glideMixin 的 mixin並定義一個 glide 方法。 然後使用 glideMixin 來給 birdboat 賦予滑行glide的能力。

--hints--

你應該聲明一個變量名爲 glideMixin 的函數。

assert(typeof glideMixin === 'function');

你應該在 bird上使用 glideMixin,以提供 glide 方法。

assert(typeof bird.glide === 'function');

你應該在 boat 上使用 glideMixin,以爲其提供 glide 方法。

assert(typeof boat.glide === 'function');

--seed--

--seed-contents--

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

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

// Only change code below this line

--solutions--

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

let boat = {
  name: "Warrior",
  type: "race-boat"
};
function glideMixin (obj) {
  obj.glide = () => 'Gliding!';
}

glideMixin(bird);
glideMixin(boat);