Illusion
Illusion

Reputation: 71

Options for passing and describing arguments to js function

What other options are there for passing and using arguments in a function using an object besides these two?

Option 1:

let timerClosure = function ({
  period, // number
  funStart, // function
  funEnd,  // function
  funStartArguments = [],
  funEndArguments = [],
  warming = 0
}) {// something }

Option 2:

let timerClosure = function (timerConfigObj) {
  let period = timerConfigObj.period; // number
  let funStart = timerConfigObj.funStart; 
  let funEnd = timerConfigObj.funEnd;
  let funStartArguments = timerConfigObj.funStartArguments || [];
  let funEndArguments = timerConfigObj.funStartArguments || [];
  let warming = timerConfigObj.warming || 0;
}

Upvotes: 0

Views: 86

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1074475

Those, or other ways of spinning them, are basically it. Well, those and using an array, but if you use an array you may as well use discrete parameters, you'll have the same issue with order being significant and the problems with that as you get to more than three parameters.

Another way to spin what you have:

let timerClosure = function (timerConfigObj) {
    const {
        period, // number
        funStart, // function
        funEnd,  // function
        funStartArguments = [],
        funEndArguments = [],
        warming = 0
    } = timerConfigObj;
    // ...
};

You've said "...and describing" in the title but not the text. If that part is important to you, you can describe these more completely by using JDDoc annotations, which many IDEs can read and present to you (even if you never actually run JSDoc) when you're using the function:

/**
 * Does something nifty with timers and closures.
 *
 * @param {Object} options - Options for the nifty thing.
 * @param {number} options.period - `period` description...
 * @param {function} options.funStart - `funStart` description...
 * @param {function} options.funEnd - `funEnd` description...
 * @param {array} options.funStartArguments - `funStartArguments` description...
 * @param {array} options.funEndArguments - `funEndArguments` description...
 * @param {number} options.warning - `warning` description...
 */
let timerClosure = function ({
    period, // number
    funStart, // function
    funEnd,  // function
    funStartArguments = [],
    funEndArguments = [],
    warming = 0
}) {
    // ...
};

Similarly, if you create a TypeScript type/interface and document its properties, IDEs will show that to you as well.

/**
 * Options for `timerClosure`
 */
interface TimerClosureOptions {
    /**
     * Period description...
     */
    period: number;
    funStart: function;
    funEnd: function;
    funStartArguments?: any[];
    funEndArguments?: any[];
    warming?: number;
}

/**
 * Does something nifty with timers and closures.
 *
 * @param {TimerClosureOptions} options - Options for the nifty thing.
 */
let timerClosure = function ({
    period,
    funStart,
    funEnd,
    funStartArguments = [],
    funEndArguments = [],
    warming = 0
}: TimerClosureOptions) {
    // ...
};

Upvotes: 1

Related Questions