2018-09-30 23:01:58 +01:00
---
id: 587d7db2367417b2b2512b89
title: Use a Mixin to Add Common Behavior Between Unrelated Objects
challengeType: 1
2019-08-05 09:17:33 -07:00
forumTopicId: 301331
2021-01-13 03:31:00 +01:00
dashedName: use-a-mixin-to-add-common-behavior-between-unrelated-objects
2018-09-30 23:01:58 +01:00
---
2020-11-27 19:02:05 +01:00
# --description--
As you have seen, behavior is shared through inheritance. However, there are cases when inheritance is not the best solution. Inheritance does not work well for unrelated objects like `Bird` and `Airplane` . They can both fly, but a `Bird` is not a type of `Airplane` and vice versa.
2019-10-27 15:45:37 -01:00
For unrelated objects, it's better to use < dfn > mixins< / dfn > . A mixin allows other objects to use a collection of functions.
2019-05-17 06:20:30 -07:00
```js
let flyMixin = function(obj) {
obj.fly = function() {
console.log("Flying, wooosh!");
}
};
```
2020-11-27 19:02:05 +01:00
The `flyMixin` takes any object and gives it the `fly` method.
2019-05-17 06:20:30 -07:00
```js
let bird = {
name: "Donald",
numLegs: 2
};
let plane = {
model: "777",
numPassengers: 524
};
flyMixin(bird);
flyMixin(plane);
```
2020-11-27 19:02:05 +01:00
Here `bird` and `plane` are passed into `flyMixin` , which then assigns the `fly` function to each object. Now `bird` and `plane` can both fly:
2019-05-17 06:20:30 -07:00
```js
bird.fly(); // prints "Flying, wooosh!"
plane.fly(); // prints "Flying, wooosh!"
```
2020-11-27 19:02:05 +01:00
Note how the mixin allows for the same `fly` method to be reused by unrelated objects `bird` and `plane` .
# --instructions--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
Create a mixin named `glideMixin` that defines a method named `glide` . Then use the `glideMixin` to give both `bird` and `boat` the ability to glide.
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
# --hints--
Your code should declare a `glideMixin` variable that is a function.
```js
assert(typeof glideMixin === 'function');
```
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
Your code should use the `glideMixin` on the `bird` object to give it the `glide` method.
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
assert(typeof bird.glide === 'function');
2018-09-30 23:01:58 +01:00
```
2020-11-27 19:02:05 +01:00
Your code should use the `glideMixin` on the `boat` object to give it the `glide` method.
```js
assert(typeof boat.glide === 'function');
```
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
# --seed--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
## --seed-contents--
2018-09-30 23:01:58 +01:00
```js
let bird = {
name: "Donald",
numLegs: 2
};
let boat = {
name: "Warrior",
type: "race-boat"
};
2020-03-08 07:46:28 -07:00
// Only change code below this line
2018-09-30 23:01:58 +01:00
```
2020-11-27 19:02:05 +01:00
# --solutions--
2018-09-30 23:01:58 +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);
```