1

I have two methods... one is basically an overload or convenience of the second. How can I easily default all of the required parameters of the convenience method?

For example, the main method is:

var doIt = function (item, varA, varB, varC, varD, callback) {
  // Lots of cool stuff in here... trust me.  ;-)
};

I would like my convenience method to pass in null values for the 'var' parameters:

var doItNow = function (item, callback) {
  return doIt(item, null, null, null, null, callback);
};

However, I do NOT like using unnamed values.

I would like do something like:

var doItNow = function (item, callback) {
  var varA = varB = varC = varD = null;
  return doIt(item, varA, varB, varC, varD, callback);
};

... but WebStorm has taught me that this is a no-no. So, I am currently doing this:

var doItNow = function (item, callback) {
  var varA = null,
      varB = null,
      varC = null,
      varD = null;
  return doIt(item, varA, varB, varC, varD, callback);
};

Which is fine, but it takes longer to type. And, when you're writing a ton of code, this becomes more tedious.

Sorry for the newbie question. Thanks, in advance.

G. Deward
  • 1,542
  • 3
  • 17
  • 30
  • possible duplicate of [Handling optional parameters in javascript](http://stackoverflow.com/questions/1529077/handling-optional-parameters-in-javascript) – JJJ Sep 13 '15 at 16:50
  • 1
    Maybe change `doIt()` to accept an object with properties so you don't have to fill in property names you don't want to pass. – jfriend00 Sep 13 '15 at 16:53
  • @jfriend00 beat me to the comment. An object is your friend in this instance. – Andy Sep 13 '15 at 16:54

4 Answers4

0

I would suggest restructuring doIt() to accept an object with optional properties rather than a long list of optional arguments. This is a recommended design pattern when lot of arguments are optional and it also makes it easier to pass along a bunch of options. And, it gives you named options. Then you can pass only the properties you want to pass without a lot of extra code.

var doIt = function (item, options, callback) {
  // Lots of cool stuff in here... trust me.  ;-)
  if (options.opt1) {
     // ...
  }
};

var doItNow = function (item, callback) {
  return doIt(item, {}, callback);
};
jfriend00
  • 683,504
  • 96
  • 985
  • 979
0

You can set optional parameters in ESCMA6 like this:

function doIt(varA = null) {
  // Bunnies
}

But until that has been fully implemented I guess you could pass an array instead which you can loop over in order to make it less tedious:

function doIt(item, myArray, callback) {

  var length = myArray.length,
      i;

  for (i = 0; i < length; i++) {
    myArray[i] = null;
  }
}

Or another option as mentioned in the comments is to pass in an object.

Chrillewoodz
  • 27,055
  • 21
  • 92
  • 175
0

However, I do NOT like using unnamed values.

Why not? They are meaningless. Why should they not be unnamed?

.. but WebStorm has taught me that this is a no-no

WebStorm is correctly complaining that that syntax (var x = y = null) is defining a global variable.

There should be no problem with

var varA, varB, varC, varD;
return doIt(item, varA, varB, varC, varD, callback);

But what's the point? Personally, I think typing the nulls expresses perfectly what you are doing, and don't find typing all of them that tedious. If it really is getting on your nerves, then you could consider this:

var doItNow = function (item, callback) {
  return doIt.apply(null, [item, , , , callback]);
};

or in ES6

var doItNow = function (item, callback) {
  return doIt(...[item, , , , callback]);
};

It would be nice if JS supported "empty" parameters, as ion

doIt(item, , , , callback)

but it doesn't.

-1

Using unnamed values is the best way to go. It's less code, more clear and more efficient. I would highly suggest just using the null values as in your example:

var doItNow = function (item, callback) {
  return doIt(item, null, null, null, null, callback);
};

Also, I suppose you mean

var doItNow = function (item, callback) {
  var varA = null,
      varB = null,
      varC = null,
      varD = null;
  return doIt(item, varA, varB, varC, varD, callback);
};

rather than

var doItNow = function (item, callback) {
      var varA = null,
          varB = null,
          varC = null,
          varD = null;
      return doIt(item, null, null, null, null, callback); <-- Not using variables
    };
Mattias
  • 715
  • 1
  • 6
  • 19