55

I am using JSDoc for parameter documentation.

It is clear how to document the parameter types for many_prompts, but what is the right way to document the function it returns?

/**
 * @param {Number} - number of times to prompt
 * @return {Function(prompt{Number})} - the returned function
 */
function many_prompts(count) {
  return function(prompt) {
    for(var i=0; i < count; i++) alert(prompt);
  }
}


//Example of use:
var y  =many_prompts(3);
y('Hello World');
Alexander
  • 4,420
  • 7
  • 27
  • 42
Aminadav Glickshtein
  • 23,232
  • 12
  • 77
  • 117

4 Answers4

25

This seems to be working for me.

 /**
 * @param {Number} count - number of times to prompt
 * @return {function(): void} - the returned function
 */
  manyPrompts(count) {
      /**
       * My inner function
       *
       * @param {object} prompt Some parameter
       */
      const inner = function(prompt) {
        for (let i=0; i < count; i++) {
          alert(prompt);
        };
      };
      return inner;
  }
gforceg
  • 421
  • 5
  • 8
  • You also don't need the parentheses: `@return {function: void}` works – Mike Harrison Jun 11 '21 at 19:38
  • This seems to work in VS Code, but doesn't pick up on the inner function documentation. :( – Aaron Campbell May 17 '22 at 21:34
  • I was able to use this one with parameters inside the parentheses: `@returns {(function([string], string, *, *): void)}`, doesn't seem to be possible to name them, though, but the IDEA, at least, parsing the types of the returned function. – extempl Jun 14 '23 at 05:15
24

You can document the inner function and then reference it like so

/**
 * @param {Number} - number of times to prompt
 * @return {many_prompts~inner} - the returned function
 */
function many_prompts(count){
  /**
   * My inner function
   *
   * @param {object} prompt Some parameter
   */
  var inner = function(prompt){
    for(var i=0;i<count;i++) alert(prompt)
  };
  return inner;
}
Zippy
  • 3,826
  • 5
  • 43
  • 96
SGD
  • 1,676
  • 14
  • 18
20

The way I prefer:

/**
 * @param {number} count - number of times to prompt
 * @returns { (prompt:string) => void } - the returned function
 */
  manyPrompts(count) {
      /**
       * My inner function
       *
       * @param {object} prompt Some parameter
       */
      const inner = function(prompt) {
        for (let i=0; i < count; i++) {
          alert(prompt);
        };
      };
      return inner;
  }
Aminadav Glickshtein
  • 23,232
  • 12
  • 77
  • 117
0

This is my solution for this. I'm not describing a return in the first function and also document the inner function, which results in getting the documentation from the inner function.

/**
 * Function to create a Function with multiple prompt messages
 * @function
 * @param {Number} count - number of times to prompt
 */
function many_prompts(count) {
  /** 
   * To prompt a value multiple times
   * @function
   * @param {String} prompt - parameter to prompt
   * @return {void} prompt the input parameter
   */
  return function(prompt) {
    for(var i=0; i < count; i++) alert(prompt);
  }
}


//Example of use:
var y  = many_prompts(3);
y('Hello World');

Which is then shown e.g. in vscode like this...

For the outer function: description of the outer function in vscode

For the inner function:

description of the inner function in vscode


You can also add an additional description when assigning the function, to describe the difference

/** 
 * Function to prompt a value 3 times
 * @function
 * @param {Number} prompt - parameter to prompt
 * @return {void} prompt the input parameter
 */
const y = many_prompts(3);
y('Hello World');
Magraina
  • 26
  • 5