Created
December 10, 2012 23:19
-
-
Save davidchambers/4254251 to your computer and use it in GitHub Desktop.
Effortlessly extend class methods in CoffeeScript
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
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