Created
August 4, 2013 19:01
-
-
Save yosiat/6151472 to your computer and use it in GitHub Desktop.
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
function one_arity(first_arg) { | |
console.log("one_arity: " + first_arg); | |
} | |
function two_arity(first_arg, second_arg) { | |
console.log("two_arity: " + first_arg, second_arg); | |
} | |
function caller() { | |
var method_to_call = null, | |
mappings = {}; | |
mappings[[typeof(1)]] = one_arity; | |
mappings[[typeof(1), typeof("a")]] = two_arity; | |
// get the types | |
var types = [], | |
args = Array.prototype.slice.call(arguments, 0); | |
args.forEach(function(value, idx) { types[idx] = typeof(value); }); | |
// get the method and invoke it | |
var method = mappings[types]; | |
method.apply(method, arguments); | |
} | |
caller(1); | |
caller(1,"2"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment