Skip to content

Instantly share code, notes, and snippets.

@JasonBBelcher
Created July 22, 2019 19:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JasonBBelcher/7b6eabad79486d4c118bb3f51db63310 to your computer and use it in GitHub Desktop.
Save JasonBBelcher/7b6eabad79486d4c118bb3f51db63310 to your computer and use it in GitHub Desktop.
Inheritance and composition without classes.
// inherit stuff without classes
function Dinosaur(name) {
function poo() {
console.log(`${name} poo poos.`);
}
function bite() {
console.log(`${name} bites and chomps!`);
}
return { poo, bite };
}
// Dragons are magical so the poop rainbows
function Dragon(name) {
const { bite } = Dinosaur(name);
function poo() {
console.log(`${name} poos rainbows and confetti`);
}
function breathFire() {
console.log(`${name} breathes fire and roasts you! `);
}
return { breathFire, bite, poo };
}
const XerxesTheDragon = Dragon("Xerxes");
XerxesTheDragon.bite();
XerxesTheDragon.breathFire();
XerxesTheDragon.poo();
function FlyingDinosaur(name) {
const proto = Dinosaur(name);
function fly() {
console.log(`${name} flaps it's wings and takes off into the air!`);
}
return Object.assign({}, proto, { fly });
}
function FlyingDragon(name) {
const { fly } = FlyingDinosaur(name);
const proto = Dragon(name);
return Object.assign({}, proto, { fly });
}
const PuffyD = FlyingDragon("Puff the Magic Dragon");
PuffyD.bite();
PuffyD.fly();
PuffyD.poo();
const SamTheDino = Dinosaur("Sam");
SamTheDino.bite();
SamTheDino.poo();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment