Java Reference
In-Depth Information
We can use this idea to add super powers to our superhero objects used earlier. All the su-
perheroes are superhuman, so they inherited any common traits from a Superhuman pro-
totype object. But they also have super-powers, and each superhero has a different mix of
powers. This is a perfect use case for mixin objects: we can create some super-power mixin
objects that can then be added to any of our super hero objects as required:
flight = {
fly: function() {
console.log(Up, up and away! " + this.name + " soars
through the
air!");
return this;
}
}
superSpeed = {
move: function() {
console.log(this.name + " can move faster than a
speeding
bullet!");
return this;
}
}
xRayVision = {
xray: function() {
console.log(this.name + " can see right through you!");
return this;
}
}
Now the relevant super powers can be added in a modular fashion to each of the super hero
objects using the mixin() method:
superman.mixin(flight,superSpeed,xRayVision);
wonderwoman.mixin(flight,superSpeed);
Search WWH ::




Custom Search