Last active
April 18, 2022 08:54
-
-
Save Node0/4c10f1ca2ad9af97b55a56aa044afb24 to your computer and use it in GitHub Desktop.
ES6 classes with slick Mixin support!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
const cLog = console.log, | |
Mixin = Object.assign; | |
function makeMixable(ctx, className) { | |
/* Automatically enumerates functions of a class and adds them as properties under this of that class's constructor. | |
* Which makes these methods accessible via the Mixin pattern. | |
*/ | |
Object.getOwnPropertyNames(className.prototype).forEach( (method) => { if (method !== 'constructor') { ctx[method] = ctx[method]; } }) | |
}; | |
function listMethodsOf(obj) { return Object.getOwnPropertyNames(obj).filter(item => typeof obj[item] === 'function') } | |
class Eukaryote { | |
constructor(radius) { | |
this.radius = | |
typeof radius != "undefined" && typeof radius == "number" | |
? radius * 2 | |
: 1; | |
this.hasNucleus = true; | |
this.nucleus = {}; | |
this.hasNanoProbe = false; | |
makeMixable(this, Eukaryote); | |
} | |
publicRadius = this.radius; | |
foo = "Some public property"; | |
cellImportinTransport(rna) { | |
if (typeof rna != "undefined") { | |
this.nucleus["importedRna"] = rna; | |
return `RNA ${this.nucleus["importedRna"]} was imported`; | |
} | |
} | |
} | |
class Bacteria { | |
constructor(radius) { | |
this.radius = (typeof radius != "undefined" && typeof radius == "number") ? radius : 1; | |
this.hasNucleus = false; | |
this.toughCellWall = {}; | |
makeMixable(this, Bacteria); | |
} | |
createHydrocarbon() { | |
console.log("This trait allows consumption of Co2"); | |
} | |
gurgle() { | |
return "gurgle"; | |
} | |
} | |
class NanoProbe { | |
constructor() { | |
this.hasNanoProbe = true; | |
this.borgProperty1 = "Some value"; | |
this.borgCapability1 = function (input) { | |
return typeof input == "number" ? input + 1 : input; | |
} | |
makeMixable(this, NanoProbe); | |
} | |
beep() { | |
return "boop"; | |
} | |
// The last property to be mixed-in via Object.assign(this, new Foo()) | |
// Will overwrite any same-named property (and likely method) that might | |
// have been defined previously. | |
logCellActivity(msg) { | |
console.log(msg); | |
} | |
} | |
class Animal extends Eukaryote { | |
constructor(name, radius) { | |
super(radius); | |
Mixin( this.NP = {}, new NanoProbe() ); | |
Mixin( this.Bact = {}, new Bacteria() ); | |
//Mixin(this.<namespace> = {}, new <ClassName>() ); | |
this.name = name; | |
} | |
identify() { | |
console.log( listMethodsOf(this.Bact) ); | |
console.log( listMethodsOf(this.NP) ); | |
// Out of the box inherited methods from Eukaryote... | |
console.log( | |
`This Animal's name is ${this.name}, its cells have a radius of ${this.radius}, | |
${this.cellImportinTransport("CAGTA")}, there is also ${this.foo}` | |
); | |
// After adding mixins... | |
if (this.NP.hasNanoProbe) { | |
console.log( | |
`Alert!!! WE ARE BORG! ${this.NP.beep()} RESISTANCE IS FUTILE, YOU WILL BE ASSIMILATED! | |
But wait, there's more, our Bacterial traits include: ${this.Bact.gurgle()}` | |
); | |
} | |
} | |
} | |
var seven = new Animal("Seven", 5); | |
seven.identify(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment