Skip to content

Instantly share code, notes, and snippets.

@yosiat
Created August 4, 2013 19:01
Show Gist options
  • Save yosiat/6151472 to your computer and use it in GitHub Desktop.
Save yosiat/6151472 to your computer and use it in GitHub Desktop.
overloading
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