Home > Software design >  How to docstring a function arugument which is itself a function with arguments in JavaScript?
How to docstring a function arugument which is itself a function with arguments in JavaScript?

Time:03-18

How can I docstring an argument which is a function itself?

Example:

/**
 * 
 * @param secondFunction // I want to say this should be a function that accepts a number
 */
function firstFunction(secondFunction) {
    const a = 1;
    secondFunction(a);
}

Cheers!

CodePudding user response:

From the JSDoc documentation:

Callback functions

If a parameter accepts a callback function, you can use the @callback tag to define a callback type, then include the callback type in the @param tag.

Parameters that accept a callback

/**
 * This callback type is called `secondFunction` and is displayed as a global symbol.
 *
 * @callback secondFunction
 * @param {number} a
 */

/**
 * executes secondFunction
 * @param {secondFunction} secondFunction - The callback
 */
function firstFunction(secondFunction) {
    const a = 1;
    secondFunction(a);
};

CodePudding user response:

You can define the type of the parameter to be the function signature you expect to be passed:

/** Calls second function with 1
 * @param {(a:number)=>void} secondFunction
 */
function firstFunction(secondFunction) {
    const a = 1;
    secondFunction(a);
};
  • Related