Last active
March 23, 2017 18:06
-
-
Save joseym/03575d1723a76ced085c2158f5ed4d0e to your computer and use it in GitHub Desktop.
One method to add as many mixins as you'd like to your NodeJS Classes
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
// Items we'd like to have mixed into our class | |
const { EventEmitter } = require('events'); | |
const { Writable } = require('stream'); | |
// ... moar | |
// Create the mixin mixer, we'll extend this. always. | |
const Mixin = (...args) => { | |
// Parent class, this constant needs to return a constructor | |
// so we'll have the constructor map all of our classes into it | |
return class Mixin { | |
constructor(){ | |
args.forEach(Class => Object.assign(this, Class.prototype)); | |
} | |
}; | |
} | |
// Usage ... | |
class NewClass extends Mixin(EventEmitter, Writable, ..., ...) { | |
constructor() { | |
super(); | |
// we now have the methods from EventEmitter and Writable. | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment