Last active
December 10, 2015 23:08
-
-
Save jkarmel/4506697 to your computer and use it in GitHub Desktop.
CoffeeScript Mixins, so easy to implement.
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
mixin = (Klass, Mixin) -> | |
# Class methods | |
Klass[key] = val for key, val of Mixin | |
# Instance methods | |
Klass::[key] = val for key, val of Mixin.prototype | |
class Mixin | |
@fn: -> alert "class fn" | |
fn: -> alert "instance fn" | |
class Base | |
baseClassFn: -> 'baseClassFn called' | |
baseInstancefn: -> 'baseInstanceFn called' | |
mixin Base, Mixin | |
Base.fn() #'class fn' alerted | |
b = new Base | |
b.fn() #'instance fn' alerted |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment