Mixins through class expressions

Jan 12, 2016  

I’ve just came across Justin’s Fagnani blog post. The basic idea is simple. Use class as an expression which returns a new class while also using an expression for the extends clause:

let Mixin = (base) => class extends base {
  hello () {
    console.log ('Mixin saying hello...');
  }
};

class BaseClass { /* ... */ }

class ClassWithMixin extends Mixin (BaseClass) { /* ... */ }

I’ve been using this pattern in Electrum to extend a component by creating an (anonymous) class which extends the provided input class:

export default function extendComponent (component /* ... */) { 
  /* ... */ 
  return class extends component { 
    constructor (props) { 
      super (props); 
      /* ... */; 
    }
  }
}