3

I have two JavaScript functions with same name but different function definition (one is parameter less and other one with two parameters). When I try to invoke parameter less function from code-behind, it always call parameterized function. But when i remove paramterized function then the function with no parameters is getting invoked. I want to know why this happening:

e.g;

<script>
function A()
{
  alert(1);
}
function A(param1 , param2)
{
  alert(2);
}
</script>

from code-behind:

Page.ClientScript.RegisterStartupScript(this.GetType(), "CallMyFunction", "A()", true);

Result: aler(2);

4
  • 5
    function overloading is not supported in javascript Commented Feb 28, 2017 at 7:05
  • You can't have two functions with the same name in Javascript Commented Feb 28, 2017 at 7:05
  • result seems fine to me. That is the standard behavior of javascript. below one is overridden the first one. Commented Feb 28, 2017 at 7:05
  • seems like you are trying to overload the function which is not possible in js. it will overwrite the function each time and you will always get the last value. Commented Feb 28, 2017 at 7:07

4 Answers 4

5

JavaScript doesn't support having two functions with the same name and different parameters. Since everything is an object only the name matters. Parameters are just metadata for the function.

You will have to have different names for those functions for this to work.

Sign up to request clarification or add additional context in comments.

2 Comments

You will have to have different names i don't think so. there are if checks and switch cases too.
@Jai And they won't help at all with calling a different function. Sure, you can make a function that checks which function you want to call, but that's kinda pointless when you can just call the actual function directly...
0

Function overloading is not supported in Javascript.

Refer the accepted answer of this Stack Overflow question which might help you.

Comments

0

If you want to achieve function overloading in JavaScript, you can use the arguments object that every function in JavaScript has. An arguments object is nothing but an array like object that contains the arguments/parameters passed to the function. You might have to change the approach a bit though.

Try this:

function A() {
    if(arguments.length === 1) {
        // code for one argument goes here
    }
    if(arguments.length === 2) {
        // code for two arguments goes here
    }
    // your further logic if needed
}

Now you can call your function A with different parameters, like so:

A(1); // code for arguments.length === 1 gets executed
A(1, 2); // code for arguments.length === 2 gets executed

Note: This is not exactly function overloading, but instead tries to work around a way of not having one.

Hope this helps.

Comments

0

Actually, the real behavior is - Javascript will override all previously written functions with same names(even if they have different no/type of parameters) with the last function. So, whatever parameters you provide in function call - it will always be the last function which will be called.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.