Skip to content

Instantly share code, notes, and snippets.

@reu
Created June 7, 2012 23:55
Show Gist options
  • Save reu/2892488 to your computer and use it in GitHub Desktop.
Save reu/2892488 to your computer and use it in GitHub Desktop.
CoffeeScript mixins
# Of course we can make something like this
class Module
@include: (mixin) ->
this.prototype[name] = method for name, method of mixin
Saveable =
save: -> alert "#{@name} saved!"
class User extends Module
@include Saveable
constructor: (@name) ->
u = new User("Navarro")
do u.save
# But what is the best way to make this work?
class User extends Module
@include Saveable
constructor: (@name) ->
save: ->
alert "Starting saving..."
super
alert "Done!"
u = new User("Navarro")
do u.save
# super obviously will not work =[
# Too tired to think of how to change the prototype chain while keeping the syntax clean and simple...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment