Function oveloading in JavaScript

I was trying to get different behavior from the function depending on its arguments, a feature found in various programming languages such as Ada, C++, C#, Delphi, D, and Java, that allows creating several methods with the same name which differ from each other in the type of the input and the output of the function” – but not in JavaScript. It has been argued that method overloading is not such a good idea (Bertrand Meyer: Overloading vs Object Technology), and in JavaScript using different function names seems to be a right way to go…

But, the interface for the user of this api in that case would look like this:

[gist https://gist.github.com/d7a8a81781ae580310de /]

but I didn’t want that. I wanted this:

[gist https://gist.github.com/6daa7b382ffb9594e0d4 /]

Turned out to be easy to add a wrapper on top of the explicitly named api:

[gist https://gist.github.com/0d17b68356bcecf5cad5 /]

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 )

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: