Created
January 12, 2013 00:52
-
-
Save jperkelens/4515343 to your computer and use it in GitHub Desktop.
method overloading based on defined params
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
function addMethod(obj, name, fn) { | |
var old = obj[name] | |
obj[name] = function() { | |
if (fn.length === arguments.length) | |
return fn.apply(this, arguments) | |
else if (typeof old == 'function') | |
return old.apply(this, arguments) | |
} | |
} | |
var myObj = {} | |
addMethod(myObj, 'doIt', function() {}) | |
addMethod(myObj, 'doIt', function(a) {}) | |
addMethod(myObj, 'doIt', function(a, b) {}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment