2

So i Have js file, with the following methods:

var DBProcessing = function()
{
console.log("ok")
...
}

var DBProcessing = function(str)
{
console.log(str);
...
}

while calling to DBProcessing() i'm getting print of undefined

is there any way to explicit call to the method without the variables in the signature?

3
  • you can't override function in js, but you can for example: var DBProcessing2 = function(str), of check parameter type inside Commented Oct 12, 2015 at 13:30
  • 2
    Why are you declaring the same function twice? Note that JavaScript doesn't differentiate functions by their method signatures in the same way the C# etc does. In your example above only the second function will be accessible. Commented Oct 12, 2015 at 13:30
  • 1
    This has nothing to do with jQuery and it by no means overriding or overloading. Although there are useful patterns that will let you achieve that. Commented Oct 12, 2015 at 13:35

3 Answers 3

7

Modify the function itelf to conditionally set str

var DBProcessing = function(str)
{
str = str || 'ok';
console.log(str);
...
}

Javascript is unlike strongly typed languages in which you must provide concrete method signatures with respect to the number and type of arguments.

In traditional strongly typed languages, you would implement method overloading like

public void OverloadedMethod()
{
}

public void OverloadedMethod(int id)
{
}

public void OverloadedMethod(int id, int age)
{
}

in which you must be very specific with regards to the number and type of arguments.

In javascript, there are no such method declarations and you can only really declare one method signature with the method accepting a variable type/number of inputs. It is up to you in the method itself how these arguments are handled.

function JSMethod(id, age){
    console.log(id);
    console.log(age);
}
JSMethod();
//outputs 'undefined', 'undefined'
JSMethod(1);
//outputs 1, 'undefined'
JSMethod(1, 26);
//outputs 1, 26

Ok, what if you passed a third argument? You can do

function JSMethod(id, age){
    console.log(id);
    console.log(age);
    console.log(arguments[2]);
}

JSMethod(1, 26, 99);
//outputs 1, 26, 99

This is because each method has an arguments object containing all the passed parameters the callee provided.

However, best practice dictates that you make your methods intention as clear as possible. So, although a javascript method can accept a variable type/number of arguments, it is better that they are stated and set to default values if need

function JSMethod(id, age){
    id = id || 1;
    age = age || 16;
}
Sign up to request clarification or add additional context in comments.

3 Comments

@haim770, youre right, I was needlessly doing that. Thanks for pionting out :-)
@AmmarCSE Could you explain it a bit? it is working , just wondering why it is acting like that? and what would happened if i would want to have method with 2 argument, than all the calls without and with only one argument will lead to to the method with 2 argument declaration?
@USer22999299, updating my answer to provide an explanation
3

define argument but dont pass instead. You can do by following way:

    var DBProcessing = function(str)
    {
       if(str !== undefined){
          // Things to do when str is passed
       } else {
       // Things to do when str is not passed
       }
    }

Comments

2

Use the following pattern for your example. As stated you can only declare a function statement once - otherwise the last function statement will take precedence. Recommend looking at further examples in relation to method overloading in javascript especially in relation to arguments and type checking.

var DBProcessing = function(str){
   if(arguments.length===0) {
      console.log("ok")
   }
   else{
      console.log(str);
   }
}

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.