Skip to content

Instantly share code, notes, and snippets.

@changtimwu
Created November 9, 2012 09:09
Show Gist options
  • Select an option

  • Save changtimwu/4044671 to your computer and use it in GitHub Desktop.

Select an option

Save changtimwu/4044671 to your computer and use it in GitHub Desktop.
multiple inheritance in coffeescript
class Mixin
# "Class method". Augment object or class `t` with new methods.
augment: (t) ->
(t[n] = m unless n == 'augment' or !this[n].prototype?) for n, m of this
t.setup()
# When an object is augmented with at least one mixin, call this method to
# remove `mixin`.
eject: (mixin) ->
(delete this[n] if m in (p for o, p of mixin::)) for n, m of this
# Implement in your mixin to act as a constructor for mixed-in properties
setup: ->
class subHelper1 extends Mixin
#setup can act as constructor
setup: ->
@h1= "ooxoooxx"
smalltask1: ->
console.log "subHelper1 do smalltask1 for #{@nm}"
class subHelper2 extends Mixin
#setup can act as constructor
setup: ->
@h2 = "blahblah"
smalltask2: ->
console.log "subHelper2 do smalltask2 for #{@nm}"
smalltask3: ->
console.log "subHelper2 do smalltask3 for #{@nm}"
class MainProtocol
constructor: (@nm)->
subHelper1::augment this
subHelper2::augment this
bigtask: ->
@smalltask1()
@smalltask2()
@smalltask3()
mp = new MainProtocol('jj')
mp.bigtask()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment