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() {
print(10);
print(10.10);
print(“ten”);
}
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

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(‘ten’);
out(1);
out(2, ‘robot’);
out(2.5);
out(true);

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! :)

About these ads

2 comments

    1. Hahaha! Thumbs up bro! xD

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 79 other followers

%d bloggers like this: