Overloading in JavaScript

Hello everyone,

In this post I will explain how to write overloading functions in JavaScript.

So let’s get started.

I will be begin with an example from Java programming language on how overloading is achieved, and after that we will do it in JavaScript.

Java
Oh no! I forgot to explain what is Overloading…. 🙂

What can we do whenever we want to name a few methods with the same name, but with a different parameters(also called arguments)?

If we have a method already had been called by some other programmer with the name of registerUserToSystem., and we want to write another method with the same name, then we need to take care of some requirements:

  1. If the number of parameters on the two methods is the same, then at least one parameter type must be different.
  2. When number of parameters are different on the two methods then it doesn’t matter what is the type of each parameter

  1. If the two methods are the same and the only difference is the name of parameters, then it is invalid overloading
  2. If the two methods are the same and the only difference is the return type of method then this is also invalid, because Overloading ignores it.

Valid Overloading

Of course valid Overloading – different number of parameters

Valid Overloading

The same count of parameters, in this case 2 parameters in each method, but in each method there is a different parameter type, in the first method we have two string, and in the second method we have one type of int and another type which is string.

Valid Overloading

Notice how for a one method we passed two parameters and to the other method we passed nothing.

Invalid Overloading

Invalid – the second method have the same types and the same count of parameters as the first method

Invalid Overloading

Invalid – The distinction between the two methods is the returned type from each method, the first return void(noting) and the second return int

Good, now we know at least what Overloading means. I hope 🙂

Now we are going to see how Overloading is achieved in JavaScript programming language.

JavaScript

At first in JavaScript you can define multiple functions with the same name without any compilation or run time error, however, the function that will be recognized is the last one in your code.
So the earlier function declarations are completely disappeared, and the last one will be used.
Why is that???
Because function in JavaScript is an Object (with additional attributes).
If you write something like this

Now we can begin with defining Overloading in JavaScript:

To JavaScript function you can pass any number of arguments, and there is no need to declare the type of each argument.
Thus, functions in JavaScript don’t actually have signatures[Signature is a function name plus number and parameters type].
So this means a lack of function overloading, because JavaScript engine cannot recognize the difference between functions with the same name,
and it will override the last function with the last earlier one, see the example below:

how we can overcome the lack of overloading in JavaScript?

As human mind we can recognize the function we need to execute by looking at the parameters number or/and the type of each one of them.
So in JavaScript each parameters you pass to any function can be read in the function even though the parameter in the function signature is not explicitly written physically, by using arguments (built-in JavaScript), for example:

Click here for more information about arguments

Here a simple example of using Overloading in JavaScript:

I hope this post helped you in something.
If you see anything that need to be fixed and updated, comments are welcome.

mhmd

Hello, I'm Mohammad Daka, software engineer who love coding. In my spare time I blogs about JavaScript,PHP, Chrome Extensions, helpful resources and tools, I'm also working on my own projects. Don't hesitate to contact me at my twitter account: @mohammad_daka :)

You may also like...

1 Response

  1. backupbot says:

    To JavaScript function you can pass any number of arguments, and there is no need to declare the type of each argument.
    Thus, functions in JavaScript don’t actually have signaturesmysql backup

Leave a Reply

Your email address will not be published. Required fields are marked *