Skip to content

Instantly share code, notes, and snippets.

@vdebergue
Created April 18, 2013 18:25
Show Gist options
  • Save vdebergue/5415048 to your computer and use it in GitHub Desktop.
Save vdebergue/5415048 to your computer and use it in GitHub Desktop.
Example of how to do Mixins in Coffeescript similar to the Scala ones
class Mixable
@with: (classes...) ->
for clazz in classes
# add the class properties
for key, value of clazz
@[key] = value
# add the instance (prototype) properties
for key, value of clazz::
@::[key] = value
class A
@id = "aze"
@find : -> "A.find"
a : -> "a"
ba : -> "a"
class B
b : -> "b"
ba : -> "b"
class BA extends Mixable
@with A, B
ba = new BA()
console.log ba.a() # "a"
console.log BA.find() # "A.find"
console.log BA.id # "aze"
console.log ba.b() # "b"
console.log ba.ba() # "b" B is the last class to the right so its definition is selected for the BA class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment