Created
April 18, 2013 18:25
-
-
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
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 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