Skip to content

Instantly share code, notes, and snippets.

@jperkelens
Created January 12, 2013 00:52
Show Gist options
  • Save jperkelens/4515343 to your computer and use it in GitHub Desktop.
Save jperkelens/4515343 to your computer and use it in GitHub Desktop.
method overloading based on defined params
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