0

In following example is a typical function which accepts three parameters:

function cook(a, b, c) {
  // cooking stuff..
  return[results];
};

Or as a property function, like this:

var myApp = {
  handler: function(a, b, c) {
    // actions
  }
};

My question is how to call this function properly, if we want to pass
only two parameters, or even only one:
Like this:

cook(param1, param2);  -  myApp.handler(param1, param2);

Or we have to pass always the same number of parameters the function
accepts, regardless if they have data or not, like this:

cook(param1, param2, "");  -  myApp.handler(param1, param2, "");

Also, what is the proper way if we want to pass the first and the third
parameters? Or only the second or the third parameter. I can't think
something other than this:

cook(param1, "", param3);  -  myApp.handler(param1, "", param3);
cook("", param2, "");
cook("", "", param3);

Is this correct and the only way to do it?

dhilt
  • 18,707
  • 8
  • 70
  • 85
cicada
  • 13
  • 2
  • 4
  • 3
    Possible duplicate of [Skipping optional function parameters in JavaScript](https://stackoverflow.com/questions/8356227/skipping-optional-function-parameters-in-javascript) – sliptype Aug 15 '18 at 21:28
  • Design it in a better way,pass a object or array instead of this. – Atul Aug 15 '18 at 21:30

3 Answers3

0

Do we have to pass always the same number of parameters the function ?

No, you don't have to pass all the parameters to a function, the arguments you don't set will be undefined

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

cook('foo') // will console log 'foo undefined undefined'

What is the proper way if we want to pass the first and the third parameters?

You were right on this one, although you would generally give undefined or null value as a parameter here rather than an empty string. If you want to ignore param2 but give param3 for example

cook(param1, null, param3)

Then you could test if all the parameters are properly set or not in your function with a more standard null or undefined value

Egg
  • 39
  • 1
  • 4
0

the best way as you describe your function would be using an object as a parameter

// function definition
function cook(object) {
    //do stuff
    return object.a + " " + object.b + " " + object.c;
}


parameters1 = {a:"paramA", b:"paramB",c:"paramC"}
cook(parameters1) // returns paramA paramB paramC

if you test those parameters in the function to handle cases where a, b or c might be null or undefined, you can then do

cook({a:"paramAonly"}) or

cook({a:"paramA", b:"paramB"})

user9748360
  • 178
  • 5
0

"My question is how to call this function properly, if we want to pass only two parameters, or even only one"

Ok. This question has multiple ways to interpret what you are asking, so I will give you one of those ways.

If you want to have a function that takes a varying number of parameters then you should use es6 spread operator like this

function (...args) { // the ... is the spread operator
  return args
}

Now if you want to return always the first item in the parameters you do this:

function (...args) {
     return args[0] 
}

Lastly if you want to do something that is more flexible then you can offer this method:

function (... args) {
  if(args.length === 3) {
     return // you get the point?
   }
}

Does this answer your question?

misterhtmlcss
  • 363
  • 1
  • 16