2

Let's say I have the following function:

function fun(a,b) {
    if(a) {  //condition can be different
        console.log(a);
        // some other code
    }
    if(b) {  //condition can be different
        console.log(b);
        // some other code, not necessary same as above
    }
}

Now I know that I can call the above function like this:

fun(1,2) // a=1, b=2
fun()    // a=b=undefined
fun(1)   // a=1, b=undefined

But I want to do something like this:

fun(2)   // a=undefined, b=2

I want to pass only one param which is assigned to b and not a.
In c# it can be done like this:

fun(b: 2) // assign b=2

So is there any way to do this in JavaScript?

An approach that I have in mind is
Instead of passing two arguments pass one object that contains the arguments.
Something like this:

function fun(obj) {
    if(obj.a) {
        console.log(obj.a);
        // some other code
    }
    if(obj.b) {
        console.log(obj.b);
        // some other code, not necessary same as above
    }
}

Using the above I can pass specific params only.

But is there any approach which will not contain any modification in the function.

Note:- I don't want to pass null or undefined as the first argument and then pass the second argument.

4
  • 1
    fun(undefined, 2) // a=undefined, b=2 Commented Aug 31, 2018 at 7:02
  • 2
    Even theoretically, how would you differentiate between fun(1), which you want to assign to a, and fun(2), which you want to assign to b? Using an object is by far the best approach (destructure the parameters for less syntax noise). Commented Aug 31, 2018 at 7:02
  • 1
    I pretty much use object params all the time now, so much more flexible / extendable. And with all the new JS features like array/object destructuring & default values makes it really tidy. It also makes callee code self documenting. Commented Aug 31, 2018 at 7:18
  • 3
    JavaScript doesn't have named parameters like Python, if that's what you're looking for. Commented Aug 31, 2018 at 7:20

3 Answers 3

4

What you can do here, is to pass an options object as param of your function, where you can specify a and b as keys for your options object.

There are several JavaScript frameworks that uses similar approach especially when building modules.

This is how should be your function:

function fun(options) {
    if(options.a) {  //condition can be different
        console.log(options.a);
        // some other code
    }
    if(options.b) {  //condition can be different
        console.log(options.b);
        // some other code, not necessary same as above
    }
}

And as examples of call you can do:

fun({b: 2})
fun({a:1, b: 3})
fun({a: "a string"})
Sign up to request clarification or add additional context in comments.

Comments

1

You can use closure for this. This way, you won't have to modify your original function.

function fun(a,b) {
    if(a) {  //condition can be different
        console.log(a);
        // some other code
    }
    if(b) {  //condition can be different
        console.log(b);
        // some other code, not necessary same as above
    }
}

function func(a) {
  return function(b) {
    fun(a,b);
  }
}

func()(2);

Comments

-1

Here's how you can achieve the result without specifically assigning the undefined value:

function fun(obj) {
  console.log(`a=${obj.a}, b=${obj.b}`)
};

fun({b: 2});

6 Comments

OP has mentioned that he does not want to pass null or undefined
@BoyWithSilverWings Thanks for mentioning. Working on it!
And what's new in your answer, you just made an edit to take the solution on my answer?
@chŝdk I don't usually look at other's answers before correcting / answering mine.
@KaranDhir Oh, that's great !! But you should do that, it's the first rule in SO.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.