--- id: 587d7db2367417b2b2512b89 title: Use a Mixin to Add Common Behavior Between Unrelated Objects localeTitle: Use un Mixin para agregar un comportamiento común entre objetos no relacionados challengeType: 1 --- ## Description
Como has visto, el comportamiento se comparte a través de la herencia. Sin embargo, hay casos en que la herencia no es la mejor solución. La herencia no funciona bien para objetos no relacionados como Bird y Airplane . Ambos pueden volar, pero un Bird no es un tipo de Airplane y viceversa. Para objetos no relacionados, es mejor usar mixins . Una mixin permite que otros objetos usen una colección de funciones.
let flyMixin = function(obj) {
  obj.fly = function() {
    console.log("Flying, wooosh!");
  }
};
El flyMixin toma cualquier objeto y le da el método de fly .
let bird = {
  name: "Donald",
  numLegs: 2
};

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

flyMixin(bird);
flyMixin(plane);
Aquí las bird y el plane pasan a flyMixin , que luego asigna la función de fly a cada objeto. Ahora el bird y el plane pueden volar:
bird.fly(); // prints "Flying, wooosh!"
plane.fly(); // prints "Flying, wooosh!"
Observe cómo la mixin permite que el mismo método de fly sea ​​reutilizado por objetos no relacionados, bird y plane .
## Instructions
Crear un mixin llamado glideMixin que define un método llamado glide . Luego use el glideMixin para que tanto el bird como el boat puedan deslizarse.
## Tests
```yml tests: - text: Su código debe declarar una variable glideMixin que es una función. testString: 'assert(typeof glideMixin === "function", "Your code should declare a glideMixin variable that is a function.");' - text: Su código debe usar el glideMixin en el objeto bird para darle el método de glide . testString: 'assert(typeof bird.glide === "function", "Your code should use the glideMixin on the bird object to give it the glide method.");' - text: Su código debe usar el glideMixin en el objeto del boat para darle el método de glide . testString: 'assert(typeof boat.glide === "function", "Your code should use the glideMixin on the boat object to give it the glide method.");' ```
## Challenge Seed
```js let bird = { name: "Donald", numLegs: 2 }; let boat = { name: "Warrior", type: "race-boat" }; // Add your code below this line ```
## Solution
```js let bird = { name: "Donald", numLegs: 2 }; let boat = { name: "Warrior", type: "race-boat" }; function glideMixin (obj) { obj.glide = () => 'Gliding!'; } glideMixin(bird); glideMixin(boat); ```