Skip to content

Instantly share code, notes, and snippets.

@joshed-io
Created May 31, 2012 22:28
Show Gist options
  • Select an option

  • Save joshed-io/2846778 to your computer and use it in GitHub Desktop.

Select an option

Save joshed-io/2846778 to your computer and use it in GitHub Desktop.
Coffeescript mixins pattern
horse = new Horse()
horse.read({ title: "The 7 Habits of Highly Successful Horses"})
horse.ebooksRead() # 1
horse.smarterThan(new Horse()) # true
this.extend = (obj, mixin) ->
for name, method of mixin
obj[name] = method
this.include = (klass, mixin) ->
extend(klass.prototype, mixin)
include(
class Horse extends Animal
legs: 4
eyes: 2
smarterThan: (readsEbooks) ->
self.ebooksRead() > readsEbooks.ebooksRead()
, ReadsEbooks)
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