Created
May 31, 2012 22:28
-
-
Save joshed-io/2846778 to your computer and use it in GitHub Desktop.
Coffeescript mixins pattern
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
| horse = new Horse() | |
| horse.read({ title: "The 7 Habits of Highly Successful Horses"}) | |
| horse.ebooksRead() # 1 | |
| horse.smarterThan(new Horse()) # true |
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
| this.extend = (obj, mixin) -> | |
| for name, method of mixin | |
| obj[name] = method | |
| this.include = (klass, mixin) -> | |
| extend(klass.prototype, mixin) |
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
| include( | |
| class Horse extends Animal | |
| legs: 4 | |
| eyes: 2 | |
| smarterThan: (readsEbooks) -> | |
| self.ebooksRead() > readsEbooks.ebooksRead() | |
| , ReadsEbooks) |
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
| this.ReadsEbooks = | |
| ebooksRead: -> | |
| @readCt | |
| read: (ebook) -> | |
| @readCt++ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment