Skip to content

Instantly share code, notes, and snippets.

@stamat
Last active December 16, 2015 00:49
Show Gist options
  • Save stamat/5350676 to your computer and use it in GitHub Desktop.
Save stamat/5350676 to your computer and use it in GitHub Desktop.
JavaScript function overloading
var def = function(functions, parent) {
return function() {
var types = [];
var args = [];
eachArg(arguments, function(i, elem) {
args.push(elem);
types.push(whatis(elem));
});
if(functions.hasOwnProperty(types.join())) {
return functions[types.join()].apply(parent, args);
} else {
if (typeof functions === 'function')
return functions.apply(parent, args);
if (functions.hasOwnProperty('default'))
return functions['default'].apply(parent, args);
}
};
};
var eachArg = function(args, fn) {
var i = 0;
while (args.hasOwnProperty(i)) {
if(fn !== undefined)
fn(i, args[i]);
i++;
}
return i-1;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment