Skip to content

Instantly share code, notes, and snippets.

@davidchambers
Created December 10, 2012 23:19
Show Gist options
  • Save davidchambers/4254251 to your computer and use it in GitHub Desktop.
Save davidchambers/4254251 to your computer and use it in GitHub Desktop.
Effortlessly extend class methods in CoffeeScript
decorate = (klass, methodName, fn) ->
method = klass::[methodName]
klass::[methodName] = (args...) ->
method.apply this, args
fn.apply this, args
# Example
decorate Array, 'push', (args...) ->
console.log "pushing #{args.length} items: #{args.join(', ')}"
array = []
array.push 'foo', 'bar', 'baz' # => pushing 3 items: foo, bar, baz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment