Skip to content

Instantly share code, notes, and snippets.

@cbrunnkvist
Last active March 22, 2017 09:52
Show Gist options
  • Save cbrunnkvist/d36c2dc7253e55e458b2af7ed5e674b8 to your computer and use it in GitHub Desktop.
Save cbrunnkvist/d36c2dc7253e55e458b2af7ed5e674b8 to your computer and use it in GitHub Desktop.
ES6 composition: mixins / traits using anonymous subclass factories
class Fig { constructor() { console.log("I'm Ficus carica") } }
const Dateable = (SC) => class extends SC { constructor() { super(); this.creationDate = new Date() } }
const Invisible = (SC) => class extends SC { isVisible() { return false } }
class InvisibleDateableFig extends Invisible(Dateable(Fig)) {}
let f = new InvisibleDateableFig()
// > f
// InvisibleDateableFig { creationDate: 2017-01-26T04:28:32.313Z }
// > f instanceof Fig
// true
// > f.isVisible()
// false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment