Multiple-Inheritance does not work in JS.
Instead of building convoluted inheritance chains, though, why not try something like Mix-Ins / Traits?
Instead of trying to figure out if WalkingSpeakingPet
should inherit from WalkingPet
or SpeakingPet
, you can instead use Traits, and add them to a pet.
function Walker (target) {
return {
walk (x, y) { console.log("I'm walking!"); }
};
}
function Flyer (target) {
return {
fly (x, y, z) { console.log("I'm flying!"); }
};
}
function Swimmer (target) {
return {
swim (x, y, z) { console.log("I'm swimming!"); }
};
}
function Pet (type, name) {
return { type, name };
}
function Dog (name) {
const dog = Pet("Dog", name);
return Object.assign(dog, Walker(dog));
}
function Parakeet (name) {
const parakeet = Pet("Bird", name);
return Object.assign(parakeet, Flyer(parakeet));
}
function HarveyTheWonderHamster () {
const harvey = Pet("Hamster", "Harvey");
return Object.assign(harvey, Walker(harvey), Flyer(harvey), Swimmer(harvey));
}