Last active
December 16, 2015 00:49
-
-
Save stamat/5350676 to your computer and use it in GitHub Desktop.
JavaScript 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 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