Skip to content

Instantly share code, notes, and snippets.

@alexlawrence
Last active January 1, 2016 04:49
Show Gist options
  • Save alexlawrence/8094534 to your computer and use it in GitHub Desktop.
Save alexlawrence/8094534 to your computer and use it in GitHub Desktop.
Function overloading.
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