JavaScript function overloading

For those who are used to Java and/or C++ it is weird that some functional languages like JavaScript and Python do not support function overloading. Even though I love dynamic functional  languages, the noted two are my favorite, I really miss this functionality, cause sometimes the functions are too small to pass variables in an object and it is too robust with ifs or whatnot and I always wished there was a more elegant way to do this. A prettier way to read and manage the code of the solution.

For those who are not sure what function overloading is, function or method overloading is declaring functions with the same name that accept different arguments thus have different behaviors depending on passed arguments.

An example in C++ would look like this (example taken from):
#include <iostream>
using namespace std;

void print(int i) {
cout << ” Here is int ” << i << endl;
void print(double f) {
cout << ” Here is float ” << f << endl;

void print(char* c) {
cout << ” Here is char* ” << c << endl;

int main() {
Which prints out:
Here is int 10
Here is float 10.1
Here is char* ten

So I have developed a simple function in JavaScript that does function overloading in a more beautiful way which is pretty to read and manage. *Note: whatis() function is described in the following post

var def = function(functions, parent) {
return function() {
var types = [];
var args = [];
eachArg(arguments, function(i, 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]);
return i1;

Demo can be found here.
The usage is very simple, pass an object with fields named as argument data types separated with comas, and pass an optional argument parent which references this in passed functions. The def function returns a closure function. When arguments are passed to this function it finds the type of arguments with whatis function and calls the correspondent function from object that lists functions passed during initialization.

Everything will be obvious from an example:
var out = def({
'int': function(a) {
alert('Here is int '+a);

‘float’: function(a) {
alert(‘Here is float ‘+a);

‘string’: function(a) {
alert(‘Here is string ‘+a);

‘int,string’: function(a, b) {
alert(‘Here is an int ‘+a+’ and a string ‘+b);
‘default’: function(obj) {
alert(‘Here is some other value ‘+ obj);


out(2, ‘robot’);

This prints out:
Here is string ten
Here is int 1
Here is an int 2 and a string robot
Here is float 2.5
Here is some other value true

I hope this simple solution helped someone produce more beautiful code and more elegant solutions. Constructive criticism is appreciated and very welcome! :)


    1. Hahaha! Thumbs up bro! xD

  1. […] tried to develop an elegant solution to this problem described here. And you can find the demo here. The usage looks like […]

  2. […] tried to develop an elegant solution to this problem described here. And you can find the demo here. The usage looks like […]

  3. […] 我试图为此处描述的问题开​​发出一种优雅的解决方案。您可以在此处找到演示。用法如下所示: […]

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: