Is it possible to name a parameter at the time of the function call?

Asked

Viewed 589 times

9

Can I "access" a specific parameter of a Javasscript function, that is, give a value for a given parameter? As an example is better than text (since I don’t know how to explain myself properly), here’s a:

In Python, having a function:

def funcX(A="A",B="B",C="C"):
    print(A+B+C)

...that would result in:

'ABC'

...I can change the value of a given parameter like this:

funcX(C = "c")

...which results in:

'ABc'

I want to know if there is any way to do the same in Javascript. Example:

function funcX(A,B,C){        
    if(typeof A != 'string'){A= "A"}
    if(typeof B != 'string'){B= "B"}
    if(typeof C != 'string'){C= "C"}

    alert(A+B+C)
};

Would result in:

'ABC'

And then I’d like to do something like:

funcX(C = "c")

...to result in:

'ABc'

Taking this opportunity, I would like to ask a parallel question (if this is allowed): There is a better way to give "default values" for the parameters of Javascript functions than if(typeof A != 'string'){A= "A"}?

As in Python def funcX(A="A")

*(sorry so much comparison, it’s just that I learned to code in Python, and it’s the only reference I have)

2 answers

7

In Javascript you really have to call the function by passing all the parameters. The function you have placed is well on the right path, if you have:

function funcX(A,B,C){        
    if(typeof A != 'string'){A= "A"}
    if(typeof B != 'string'){B= "B"}
    if(typeof C != 'string'){C= "C"}

    alert(A+B+C)
};

so if you just want to change the C you would have to call the function with funcX(null, null, 'c');.

jsFiddle: http://jsfiddle.net/kdxerxpw/

Another way to give default values would be:

function funcX(A,B,C){        
    A = A || 'A';
    B = B || 'B';
    C = C || 'C';

    alert(A+B+C)
};

jsFiddle: http://jsfiddle.net/kdxerxpw/1/

But I think how you have it is better, more specific. In case you do not need to redefen any parameter you can call the function only with funcX();

jsFiddle: http://jsfiddle.net/kdxerxpw/2/

  • Thanks Sergio, I wish I could save typing, but unfortunately I can not pass only one parameter. Do what At least now I don’t have a doubt, I know I can’t ?

5


The usual way to make named or optional Javascript parameters is to pass an object (dictionary) as parameter. A famous example is the function ajax from the jQuery library.

In your case it would look something like this:

function funcX(kw){
    var A = kw.A || "A";
    var B = kw.B || "B";
    var C = kw.C || "C";
    console.log(A+B+C);
}

funcX({a:"oi", c:"!", b:"mundo"});

It is quite common to use || in optional JS parameters because the code is very succinct but if you want to accept "falsy" values like 0 or "" you can make more accurate tests using typeof or operator in:

 var A = "A" in kw ? kw.A : "defaultA";
  • Very good, I came back here just to comment that I just discovered that I could use an object to make parameters with "default values" (I found out using Jquery, more precisely with jQuery Ui). Doing something like funcX({C:'c'}). But actually I didn’t quite figure out how it works inside

  • 1

    There is not much magic. The Python analog would be to pass a dictionary as a parameter instead of using keyword Arguments. To implement optional parameters you can use the operator in to test if the dictionary contains a certain field (works like Python) or you go out accessing the fields directly (in Python access a non-existent field of a dictionary is a mistake but in Javascript you simply get the value undefined, which is kind of a null only a little different).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.