OOP, inheritance
by lovinglobo
JavaScript
class Animal { // super class
constructor(name, legCount) {
this.name = name;
this.legCount = legCount;
}
greet() {
console.log(`hi ${this.name}. You've got ${this.legCount} legs! :)`);
}
fuck() {
// little dynamic dispatch trick
window[`${this.type}Fuck`]();
}
}
class Dog extends Animal {
constructor(name) {
super(name, 4);
this.woofDb = "110db";
this.type = "dog";
}
greet() {
console.log(`WOOF ${this.name}. You've got ${this.legCount} legs! :)
WOOF db: ${this.woofDb}.`);
}
}
class HomoSapien extends Animal { // sub class
constructor(name) {
super(name, 2);
this.neoCortexSize = "5kg";
this.type = "homoSapien";
}
greet() {
console.log(`Hello ${this.name}. You've got ${this.legCount} legs! :)
My neo cortex weighs: ${this.neoCortexSize}.`);
}
}
function dogFuck() {
console.log("You dirty dog!");
}
function homoSapienFuck() {
console.log("You dirty homo!");
}
function undefinedFuck() {
console.log("what the fuck?");
}
const jalando = new HomoSapien("Jalando");
jalando.greet();
jalando.fuck();
const fluffles = new Dog("fluffles");
fluffles.greet();
fluffles.fuck();
const prototypicalAnimal = new Animal("proto", 0);
prototypicalAnimal.greet();
prototypicalAnimal.fuck();