Created
June 22, 2012 17:08
-
-
Save brihogan/2974018 to your computer and use it in GitHub Desktop.
CoffeeScript: Method overloading
This file contains 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
# 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