3

Let's say I have a function

function myfunction(a, b, c) {
  // do stuff
}

What is the optimal way to call this function with only param c?

a = 1;
b = 2;
c = 'hello';
myfunction(c);

I was thinking this:

myfunction(undefined, undefined, c);

But there must be a better way. I was thinking passing an object, but this function is already being used, I can't change the param structure.

15
  • write a wrapper. Also for ocd reasons, please avoid writing "most optimal". Commented Jul 10, 2018 at 14:03
  • even if no solution is supplied. ^^^^^^^^^^^^^^^^^^^^^^^^ Commented Jul 10, 2018 at 14:04
  • 1
    @ASDFGerte Not just optimal. Most optimal. Optimaller. Optimallist. Commented Jul 10, 2018 at 14:04
  • 1
    You can create a new function myfunctionc by curry-in the function myfunction: myfunctionc = c => myfunction(undefined, undefined, c); myfunctionc(c); Commented Jul 10, 2018 at 14:05
  • 3
    @kshetline because many of the answers are either plain wrong, did not read the question, or have bad and unnecessary side effects. I did not downvote any, but can totally see where it is coming from. Commented Jul 10, 2018 at 14:12

5 Answers 5

2

The best method is to define the parameters in the right order, so the last ones are the optionals, and then you will be able to use the optional arguments as defined in ES6

function(c, a=1, b=2) {
   // ...
}

Since named parameters are not supported in JS, if you have many optional parameters, replace them by a single object.

function(obj) {
   // obj.a, obj.b, obj.c
}

Alternatively you could use the arguments object in the function body.

function() {
    // arguments[0], arguments[1], arguments[2]
}

If you are not able to redesign the function, then find out the default values for the parameters, and use those. It is usually 0, an empty array or null.

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

Comments

2

You can use destructuring like below

const a = 1;
const b = 2;
const c = 'hello'

function myfunction({a,b,c}){
  console.log(c)
}

myfunction({c})


function wrapper({params}){}

Another way is to create a wrapper which will control your parameters (without function modification)

const a = 1;
const b = 2;
const c = 'hello'

function myfunction(a,b,c){
  console.log(c)
}

function wrapperForMyFunction({a, b, c}){
  return myfunction(a,b,c)
}

wrapperForMyFunction({ c })

2 Comments

I'm not the downvoter but I believe it's because the OP explicitly mentioned that he/she cannot modify the original function > "I was thinking passing an object, but this function is already being used, I can't change the param structure."
shrug I wouldn't have downvoted; I think it's a better approach than anything other than a wrapper. (I think a wrapper is best because it preserves the original method's intent, but if I'm in control of the original method, and there's a bunch of stuff that can be option, to me an object makes more sense.)
1

You can try this (use bind):

a = 1; b = 2; c = 'hello'


function myfunction(a,b,c){
  console.log('a = ', a, 'b = ', b, 'c = ', c);
}

// newFunc same myfunction but first and second parameter is undefined
// potentially incorrect if 'this' is necessary
const newFunc = myfunction.bind(null, undefined, undefined);

newFunc(c);

newFunc(c, 'something');

3 Comments

Seems overkill (and potentially incorrect if this is necessary) when you can just create a wrapper.
I change it to null, but I don't sure that . Can you tell me that is right or wrong ?
It's not "right" or "wrong", I just don't see bind as being the optimal solution--it's just a wrapper function. I didn't downvote, I just wouldn't do it this way--it's a valid solution.
0

var a = 1;
var b = 2;
var c = "hello";

myfunction({c:c})

function myfunction(a,b,c){
  if (typeof a === "object") {
      obj = a;
      a = obj.a;
      b = obj.b;
      c = obj.c;
  }
 
  // do stuff
}

this will work but "a" can never be a object, its the only issue

Comments

-1

i think you are looking for var args. please take a look.

var a = 1; b = 2; c = 'hello'
function varargsFunction1()  
{  
    console.log(arguments[2]);
}    
varargsFunction1(a,b,c); 

1 Comment

Certainly valid, but relying on position is fraught with danger.

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.