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.

cнŝdk
  • 31,391
  • 7
  • 56
  • 78
yajiv
  • 2,901
  • 2
  • 15
  • 25
  • 1
    fun(undefined, 2) // a=undefined, b=2 – Gerard Aug 31 '18 at 07: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). – CertainPerformance Aug 31 '18 at 07: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. – Keith Aug 31 '18 at 07:18
  • 3
    JavaScript doesn't have named parameters like Python, if that's what you're looking for. – Barmar Aug 31 '18 at 07:20

3 Answers3

3

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"})
cнŝdk
  • 31,391
  • 7
  • 56
  • 78
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);
Vipin Kumar
  • 6,441
  • 1
  • 19
  • 25
-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});
Lioness100
  • 8,260
  • 6
  • 18
  • 49
Karan Dhir
  • 731
  • 1
  • 6
  • 24