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) {El
obj.fly = function() {
console.log("Flying, wooosh!");
}
};
flyMixin
toma cualquier objeto y le da el método de fly
.
let bird = {Aquí las
name: "Donald",
numLegs: 2
};
let plane = {
model: "777",
numPassengers: 524
};
flyMixin(bird);
flyMixin(plane);
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!"Observe cómo la
plane.fly(); // prints "Flying, wooosh!"
mixin
permite que el mismo método de fly
sea reutilizado por objetos no relacionados, bird
y plane
.
mixin
llamado glideMixin
que define un método llamado glide
. Luego use el glideMixin
para que tanto el bird
como el boat
puedan deslizarse.
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.");'
```