<p>Open developer console to see output</p>
const AnimalPrototype = {
init(noise) {
this.noise = noise;
return this;
},
eat() {
console.log("Om nom nom");
},
sleep() {
console.log("Zzzzzzz");
},
makeNoise() {
console.log(this.noise);
}
};
const WingedCreatureMixin = {
fly() {
if (this.canFly) {
console.log("I'm flyyyying");
} else {
console.log("I can't fly, you fools!");
}
}
};
const EggLayerMixin = {
layEgg() {
console.log("*Plop*");
}
};
const MudWallowerMixin = {
wallowInMud() {
console.log("Aaaahh...");
}
};
function createBird(noise, canFly = true) {
const bird = Object.create(AnimalPrototype).init(noise);
Object.assign(bird, WingedCreatureMixin, EggLayerMixin);
bird.canFly = canFly;
return bird;
}
function createFlyingPig() {
const pig = Object.create(AnimalPrototype).init("Oink!");
Object.assign(pig, MudWallowerMixin, WingedCreatureMixin);
pig.canFly = true;
return pig;
}
const owl2 = createBird("Hoot");
const kiwi2 = createBird("Shriek!", false);
const flyingPig = createFlyingPig();
owl2.makeNoise();
owl2.fly();
owl2.layEgg();
kiwi2.makeNoise();
kiwi2.fly();
kiwi2.layEgg();
flyingPig.makeNoise();
flyingPig.wallowInMud();
flyingPig.fly();
console.log(owl2, kiwi2, flyingPig);
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.