Skip to content

Instantly share code, notes, and snippets.

@jkarmel
Last active December 10, 2015 23:08
Show Gist options
  • Save jkarmel/4506697 to your computer and use it in GitHub Desktop.
Save jkarmel/4506697 to your computer and use it in GitHub Desktop.
CoffeeScript Mixins, so easy to implement.
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