Click here to Skip to main content
15,885,278 members
Articles / Programming Languages / Javascript

JavaScript Does NOT Support Method Overloading – That's True!!

Rate me:
Please Sign up or sign in to vote.
4.73/5 (9 votes)
31 Jul 2020CPOL3 min read 60.6K   5   15
It's true that JavaScript does not support method overloading
In this post, we will take a look at an issue that people might encounter while coding in JavaScript. We will also see the solution to this problem.

A few weeks ago, I blogged about how “surprisingly” WCF Operation Contracts do not support Method overloading, due to the way the Web is designed.

It may come as a surprise to a lot of folks that even JavaScript does NOT support method overloading in the strictest sense.

For people from C# and Java background, who have just started coding in JavaScript, and are getting a hang of it, they may run into one particular issue while coding in JavaScript.

Problem

A common error/mistake almost everyone does (i.e. if the developer is keen enough to dive deep into JavaScript) is explained below.

The example has 2 JavaScript functions defined in a JS file as below. They have the same name, BUT different number of arguments (your typical method overloading).

function funcA(a,b) {
return a + b;
}

function funcA(c) {
return c;
}

Now, if we make the calls below, the outputs are as shown below:

funcA(2);  //Output is '2'

funcA(3,4); //Output is '3' (and NOT '7' !!)

So, WTH did just happen!!

The issue is that JavaScript does NOT natively support method overloading. So, if it sees/parses two or more functions with a same name, it’ll just consider the last defined function and overwrite the previous ones.

In our case, the only function available is funcA(c). So even if we think we are making a call to funcA(a,b), it's actually calling funcA(c).

So, even when we make a call as funcA(3,4), why doesn’t it throw an exception if there are mismatching number of arguments??

This is where there is another JavaScript feature, which a lot of beginners miss out on. In any JavaScript method, the arguments passed into the method are accessible via an Object array with the name args[].

So in our case, even if we define a JavaScript method as funcA(), we can still make a call to it as:

funcA(2)
funcA(2,3)
funcA(2, "hello", 4, 5)

All of the above are valid and the parameters can be accessed by Index in the args[] array, available inside the method.

This is the reason JavaScript does not throw an exception; if you pass 2 parameters in a JavaScript function accepting only 1, it’ll just consider the first of the 2 parameters.

Solution

The easiest solution that can be applied here is that we have a parent function with the same name accepting any number of arguments. Inside this function, we inspect the incoming arguments (type, number, order, etc.) and call the appropriate child methods based on that (see below for a very simple and trivial example).

function funcA() {
if (arguments.length==1) {

return funcOne(arguments[0]);

}else if (arguments.length==2){

return funcTwo(arguments[0],  arguments[1]);

}}

function funcTwo(a,b) {
return a + b;
}

function funcOne(c) {
return c;
}

The solution suggested above may sound easy to implement, if you are dealing with basic types (integer, string, array) and are planning on overloading only a handful of functions. BUT, if your overloading requirements are complicated, you may have to put in a lot of effort and code.

There are a few efficient ways to overload in JavaScript out there, which I may discuss in a future post.

We may be used to comprehensive & extensive method overloading in common Object Oriented Languages like C# and Java, BUT JavaScript is different.

Although JavaScript can be said to be an Object Oriented language, BUT it's not a class based language as C# and Java. What it means is that apart from primitive data types like Number, String and Boolean, every other Type in JavaScript is simply of type Object.

So, even if we intend to somehow implement and use method overloading in JavaScript, it’ll be limited as compared to other OO languages.

If any of you have any suggestions on an ideal and reliable Overloading solution in JavaScript, you are welcome to provide them in the comments.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
United States United States
I am a Developer working across multiple Technologies like .NET, Scala, Angular and NodeJS.
Passionate about WEB technologies more than anything.
Fascinated about the idea of how the WEB world and the WinForm world are slowly merging into each other, with a very thin line to differentiate them.

I am an active blogger and I blog mostly about Technology. Also, I have presented at certain points in the past on technical topics.

My Blog


Life & Tech

Programming Community Profiles


Stack Overflow
GitHub

Presentations


Slideshare

Social Profiles


Facebook | Twitter | LinkedIn | Google+

Comments and Discussions

 
Questionarguments not args Pin
jsc424-Aug-20 2:58
professionaljsc424-Aug-20 2:58 
QuestionOverloading definition ! Pin
Member 109077413-Aug-20 7:50
Member 109077413-Aug-20 7:50 
GeneralMy vote of 5 Pin
Raul Iloc1-Aug-20 6:31
Raul Iloc1-Aug-20 6:31 
SuggestionOverloading is possible, you just need to set it up yourself Pin
Member 122256538-Aug-16 9:49
Member 122256538-Aug-16 9:49 
GeneralRe: Overloading is possible, you just need to set it up yourself Pin
Chinmoy Mohanty13-Oct-16 20:40
Chinmoy Mohanty13-Oct-16 20:40 
GeneralRe: Overloading is possible, you just need to set it up yourself Pin
Giorgio Arata6-Aug-20 4:15
professionalGiorgio Arata6-Aug-20 4:15 
QuestionA JS array is a JS object Pin
Gerd Wagner27-Jul-16 7:30
professionalGerd Wagner27-Jul-16 7:30 
AnswerRe: A JS array is a JS object Pin
Chinmoy Mohanty5-Aug-16 5:41
Chinmoy Mohanty5-Aug-16 5:41 
GeneralRe: A JS array is a JS object Pin
Gerd Wagner5-Aug-16 12:38
professionalGerd Wagner5-Aug-16 12:38 
GeneralGood one Pin
Vignesh Mani27-Jul-16 1:19
professionalVignesh Mani27-Jul-16 1:19 
GeneralRe: Good one Pin
Chinmoy Mohanty5-Aug-16 5:42
Chinmoy Mohanty5-Aug-16 5:42 
BugPlease fix you xml encoding! Pin
Petoj8727-Jul-16 1:05
Petoj8727-Jul-16 1:05 
GeneralRe: Please fix you xml encoding! Pin
Chinmoy Mohanty5-Aug-16 5:43
Chinmoy Mohanty5-Aug-16 5:43 
GeneralMy vote of 5 Pin
Donny Redmond22-Jul-14 4:16
Donny Redmond22-Jul-14 4:16 
GeneralTip... Pin
Nitij20-Jul-14 19:36
professionalNitij20-Jul-14 19:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.