Reputation: 7041
class A
constructor:
//dosomething
loadFunctions:
loadDrillingCharges: (memoize) ->
class B extends A
constructor:
super()
loadFunctions:
loadLockDDR: (memoize) ->
(new B).loadFunctions
will be an object with loadLockDDR
attribute only
I want the (new B).loadFunctions
to be { loadDrillingCharges: -> , loadLockDDR: -> }
I can _.extend(B::loadFunctions, A::loadFunctions)
but it is not elegant.
I tried to use cocktail mixin but it screw up the super()
What can I do to merge the object after extend and not screw up the coffescript super.
Upvotes: 0
Views: 158
Reputation: 52000
Not very sexy, but...
class A
constructor: ->
# dosomething
loadFunctions: ->
loadDrillingCharges: (memoize) ->
class B extends A
constructor: ->
super()
loadFunctions: ->
do (that=super()) ->
that.loadLockDDR = (memoize) ->
that
console.log (new A).loadFunctions()
console.log (new B).loadFunctions()
Producing:
{ loadDrillingCharges: [Function] }
{ loadDrillingCharges: [Function], loadLockDDR: [Function] }
Upvotes: 0
Reputation: 11
Mixins are not something supported natively by CoffeeScript, for the good reason that they can be trivially implemented yourself. For example, here’s two functions, extend() and include(), that’ll add class and instance properties respectively to a class:
extend = (obj, mixin) -> obj[name] = method for name, method of mixin obj include = (klass, mixin) -> extend klass.prototype, mixin # Usage include Parrot, isDeceased: true (new Parrot).isDeceased
Upvotes: 1