minglecm / coffeescript-mixins

Package to allow mixins with CoffeeScript classes.
11 stars 2 forks source link

Constructor not called when mixin inherits #2

Open shaunc opened 9 years ago

shaunc commented 9 years ago

Consider the following code:

mixins = require 'coffeescript-mixins'
mixins.bootstrap() # Mixes in include on Function

class Base

  constructor: ()->
    console.log('Foo')

class MixinBase

class Mixin extends MixinBase

class Derived extends Base
  @include Mixin

new Derived()

In this case, the constructor in Base is not called and there is no output. If the @include is commented out, or changed to @include MixinBase then the code works again.

yangmillstheory commented 9 years ago

Just a general question, but what's the value of using classes to define mixins?

Do you need to dynamically create mixin instances at runtime? It seems to me that mixins should always be singletons, so that we could always afford a simpler implementation using object literals. If a caller needed to customize mixin properties, instead of using inheritance, one could use something like _.extend to override "base" mixin behavior.

See for example this blog post.