Created
June 7, 2012 23:55
-
-
Save reu/2892488 to your computer and use it in GitHub Desktop.
CoffeeScript mixins
This file contains hidden or 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
| # 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