Skip to content

Instantly share code, notes, and snippets.

@bravoecho
Created June 26, 2014 10:32
Show Gist options
  • Save bravoecho/810c3f609ee9cef50efb to your computer and use it in GitHub Desktop.
Save bravoecho/810c3f609ee9cef50efb to your computer and use it in GitHub Desktop.
Experiment with coffeescript mixins that change the prototype
class @Moo
class @Mixin
constructor: ->
@boo = 'boo'
@mixin: (klass) ->
klass::mix = @::mix
klass::max = @::max
klass::getBoo = @::getBoo
klass::setBoo = @::setBoo
mix: ->
console.log('mix')
max: ->
console.log('max')
setBoo: (boo) ->
@boo = boo
getBoo: ->
@boo
@Mixin.mixin(@Moo)
moo = new @Moo()
moo2 = new @Moo()
console.log "\nRunning...\n"
console.log moo # Moo {mix: function, max: function, getBoo: function, setBoo: function}
moo.mix() # mix
moo.max() # max
console.log moo.getBoo() # undefined
moo.setBoo('boo')
moo2.setBoo('BOO')
console.log moo.getBoo() # boo
console.log moo2.getBoo() # BOO
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment