Luke Scott (2015-02-12T21:07:38.000Z)
d at domenic.me (2015-02-17T20:26:32.201Z)
I know what you’re going for, and I had this in my initial implementation. The issue then becomes: If Trait2 mixes in Trait1, does the init method get replaced? Traits are about inserting functionality, not about inheritance. Example: ```js trait Trait1 { init() { // init Trait1 } } trait Trait2 { mixin Trait1; init() { // init Trait 2 } } class Foo { mixin Trait2; constructor() { // do Foo stuff... } } ``` So either init is special and the call order is Trait1.init() Trait2.init() (while other methods get replaced) or Trait2.init() replaces the Trait1.init(). Also since traits are not classes, they cannot call super. You also are defying the call stack because trait initializers “just happen” internally. There is no trail to step through, unlike classes which leaves a trail of breadcrumbs with super() calls. This version is actually easy to follow: ```js trait Trait1 { initTrait1() { // init Trait1 } } trait Trait2 { mixin Trait1; initTrait2() { this.initTrait1(); // init Trait 2 } } class Foo { mixin Trait2; constructor() { this.initTrait2(); // do Foo stuff... } } ``` True, each class has to call the custom init methods. But it still addresses 95% of the problem: Adding in common functionality that doesn’t fit in classical inheritance. Also since some traits may expect to be mixed in to a subclass of a certain base, it allows you to call super before the trait initializers, and that’s something you already have to do in your local constructor, so initializing traits in the same way makes sense. The only thing extra beyond copying implementation into a class or another trait is keeping track of what traits have been mixed in for a “hasTrait” check. This isn't necessarily required as you can easily check for a method you want to call, but it does allow you to make sure that the method belongs to trait and check once for a group of methods. (This is my ES6 implementation of traits that I’m using now: https://gist.github.com/lukescott/36453a75c39c539f5c7d)