2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
id: 587d7db2367417b2b2512b89
|
2020-12-16 00:37:30 -07:00
|
|
|
|
title: 使用 Mixin 在不相关对象之间添加共同行为
|
2018-10-10 18:03:03 -04:00
|
|
|
|
challengeType: 1
|
2020-08-04 15:15:28 +08:00
|
|
|
|
forumTopicId: 301331
|
2021-01-13 03:31:00 +01:00
|
|
|
|
dashedName: use-a-mixin-to-add-common-behavior-between-unrelated-objects
|
2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --description--
|
|
|
|
|
|
|
|
|
|
正如你所见,行为是可以通过继承来共享的。然而,在有些情况下,继承不是最好的解决方案。继承不适用于不相关的对象,比如`Bird`和`Airplane`。虽然它们都可以飞行,但是`Bird`并不是一种`Airplane`,反之亦然。
|
|
|
|
|
|
|
|
|
|
对于不相关的对象,更好的方法是使用`mixins`。`mixin`允许其他对象使用函数集合。
|
2020-08-04 15:15:28 +08:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
let flyMixin = function(obj) {
|
|
|
|
|
obj.fly = function() {
|
|
|
|
|
console.log("Flying, wooosh!");
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
```
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
`flyMixin`能接受任何对象,并为其提供`fly`方法。
|
2020-08-04 15:15:28 +08:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
let bird = {
|
|
|
|
|
name: "Donald",
|
|
|
|
|
numLegs: 2
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let plane = {
|
|
|
|
|
model: "777",
|
|
|
|
|
numPassengers: 524
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
flyMixin(bird);
|
|
|
|
|
flyMixin(plane);
|
|
|
|
|
```
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
这里的`flyMixin`接收了`bird`和`plane`对象,然后将`fly`方法分配给了每一个对象。现在`bird`和`plane`都可以飞行了:
|
2020-08-04 15:15:28 +08:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
bird.fly(); // prints "Flying, wooosh!"
|
|
|
|
|
plane.fly(); // prints "Flying, wooosh!"
|
|
|
|
|
```
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
注意观察`mixin`是如何允许相同的`fly`方法被不相关的对象`bird`和`plane`重用的。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --instructions--
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
创建一个名为`glideMixin`的`mixin`,并定义一个`glide`方法。然后使用`glideMixin`来给`bird`和`boat`赋予滑行(glide)的能力。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --hints--
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
你应该声明一个变量名为`glideMixin`的函数。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
|
|
```js
|
2020-12-16 00:37:30 -07:00
|
|
|
|
assert(typeof glideMixin === 'function');
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
你应该在`bird`上使用`glideMixin`,以提供`glide`方法。
|
2020-08-04 15:15:28 +08:00
|
|
|
|
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```js
|
2020-12-16 00:37:30 -07:00
|
|
|
|
assert(typeof bird.glide === 'function');
|
|
|
|
|
```
|
2020-08-04 15:15:28 +08:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
你应该在`boat`上使用`glideMixin`,以提供`glide`方法。
|
2020-08-04 15:15:28 +08:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
```js
|
|
|
|
|
assert(typeof boat.glide === 'function');
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```
|
2020-08-04 15:15:28 +08:00
|
|
|
|
|
2021-01-13 03:31:00 +01:00
|
|
|
|
# --seed--
|
|
|
|
|
|
|
|
|
|
## --seed-contents--
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
let bird = {
|
|
|
|
|
name: "Donald",
|
|
|
|
|
numLegs: 2
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let boat = {
|
|
|
|
|
name: "Warrior",
|
|
|
|
|
type: "race-boat"
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Only change code below this line
|
|
|
|
|
```
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --solutions--
|
|
|
|
|
|
2021-01-13 03:31:00 +01:00
|
|
|
|
```js
|
|
|
|
|
let bird = {
|
|
|
|
|
name: "Donald",
|
|
|
|
|
numLegs: 2
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let boat = {
|
|
|
|
|
name: "Warrior",
|
|
|
|
|
type: "race-boat"
|
|
|
|
|
};
|
|
|
|
|
function glideMixin (obj) {
|
|
|
|
|
obj.glide = () => 'Gliding!';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
glideMixin(bird);
|
|
|
|
|
glideMixin(boat);
|
|
|
|
|
```
|