Skip to content

Instantly share code, notes, and snippets.

@joseym
Last active March 23, 2017 18:06
Show Gist options
  • Save joseym/03575d1723a76ced085c2158f5ed4d0e to your computer and use it in GitHub Desktop.
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
// 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