Skip to content

Instantly share code, notes, and snippets.

@francescoagati
Created January 5, 2012 09:54
Show Gist options
  • Save francescoagati/1564491 to your computer and use it in GitHub Desktop.
Save francescoagati/1564491 to your computer and use it in GitHub Desktop.
coffeescript: chainable methods
chained_method= (obj,method,fn) -> obj::[method]= ()-> fn.apply(@,arguments);@
class Base
constructor: -> @n=0
chained_method @,'sum', (x) -> @n=@n+x
chained_method @,'minus', (x) -> @n=@n-x
chained_method @,'pp', (x) -> console.log @n
(new Base).sum(3).minus(10).pp()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment