Created
June 26, 2014 10:32
-
-
Save bravoecho/810c3f609ee9cef50efb to your computer and use it in GitHub Desktop.
Experiment with coffeescript mixins that change the prototype
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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