Tomasz Nazar, Living Poland (Europe)
Been software developer for 10+ years
Focused on OO, domain modelling, code readability
Been working in Java, Python, a bit of Ruby.. as of end of 2011 unleashing power of Javascript via CoffeeScript cool code
So, we’ve finally achieved a small utility function that implements mixins in Coffee. In Javascript too :-)
Implementation might not yet be super clean, but it allows us to:
In short:
and implementation is bit hacky, but works. Uses underscore.js and modified hashmal’s Mixin.
One small requirement of this solution is all roles must extend Mixin class.
class Nothing
constructor: (@name=”nothing”) ->
doSomething: =>
“I cannot do anything”
class Presenter extends Mixin
presentYourself: =>
“My name is #{@name}”
doIPlay: =>
“I play with many #{@players}”
class Game extends Mixin
setup: =>
@name = ‘Monopoly’
@players = []
@players.push “David”
sayHello: =>
@presentYourself()
something = new Nothing()
expect(something.name).toBe(‘nothing’)
ObjectHelper.addRole(something, Presenter)
expect(something.presentYourself()).toBe(‘My name is nothing’)
ObjectHelper.addRole(something, Game)
expect(something.sayHello()).toBe(‘My name is Monopoly’)
expect(something.doIPlay()).toBe(‘I play with many David’)
something.name = ‘something’
expect(something.presentYourself()).toBe(‘My name is something’)
something.players.push “John”
expect(something.doIPlay()).toBe(‘I play with many David,John’)
</pre>
And stripped down whole Mixin.coffee