Multiple inheritance (with mixins)

67 views
Skip to first unread message

Maga D. Zandaqo

unread,
Dec 24, 2015, 8:22:20 AM12/24/15
to Strengthen JS
Hello,
I am looking into ways to make our in-house MV* framework strong mode compatible. My first issue is with dynamically augmenting this in constructors with a mixin implementing event based pub/sub system. We use a mixin instead of a class since classes using it may descend from buil-in classes, and ES6/2015 doesn't provide a way to have multiple inheritance. That is: 

let EventEmitter: {
  on(){}
  off(){}
  ...
}

class A extends Map {
  constructor(){
    super();
    //add EventEmitter props to this.
  }
}

class B extends Set {
  constructor(){
    super();
    //add EventEmitter props to this.
  }
}

So far the only way seems to be just listing all properties statically, since all other uses of this are forbidden. Is there any other way to do it?

Maga D. Zandaqo

unread,
Dec 25, 2015, 6:14:20 AM12/25/15
to Strengthen JS
Another workaround would be to dynamically generate EventEmitter as class descending from a given class, and then make a new class descend from the generated class:

function EventFactory(parent){
  return class EventEmitter extends parent {
    on(){}
    off(){}
    ...
  }
}

class A extends EventFactory(Map) {}

This seems to work in the current Chrome Canary with strong-mode flag, but personally I'd prefer to use a mixin instead of lengthening the prototype chain with extra classes.

Youness Belfkih

unread,
Mar 26, 2016, 2:53:26 AM3/26/16
to Strengthen JS
I'm planning on implementing mixins in a language I'm currently designing but strong JS will not allow me because o that freezing thingy so my hope is ES7 decorators https://github.com/wycats/javascript-decorators
Reply all
Reply to author
Forward
0 new messages