Last active
January 1, 2016 04:49
-
-
Save alexlawrence/8094534 to your computer and use it in GitHub Desktop.
Function 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
var overloaded = function() { | |
var functions = Array.prototype.slice.call(arguments, 0); | |
return function() { | |
try { | |
var functionToInvoke = functions.filter(function(x) { | |
return x.length == arguments.length; | |
})[0]; | |
functionToInvoke.apply(this, arguments); | |
} | |
catch (error) { throw new Error('no appropriate overload found'); } | |
}; | |
}; | |
// Example | |
var foo = overloaded(function(a) { | |
}, function(a, b) { | |
}, function(a, b, c) { | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment