Skip to content

Instantly share code, notes, and snippets.

@brihogan
Created June 22, 2012 17:08
Show Gist options
  • Save brihogan/2974018 to your computer and use it in GitHub Desktop.
Save brihogan/2974018 to your computer and use it in GitHub Desktop.
CoffeeScript: Method overloading
# Via Jone Resig: http://ejohn.org/apps/learn/#90
addMethod (object, name, fn) ->
# Save a reference to the old method
old = object[ name ]
# Overwrite the method with our new one
object[ name ] = ->
# Check the number of incoming arguments,
# compared to our overloaded function
if fn.length is arguments.length
# If there was a match, run the function
return fn.apply this, arguments
# Otherwise, fallback to the old method
else if typeof old is "function"
return old.apply this, arguments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment