How to document a function returned by a function using JSDoc

23,152

Solution 1

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;
}

Solution 2

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;
  }

Solution 3

The way I prefer:

/**
 * @param {number} count - number of times to prompt
 * @returns { (promt: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;
  }
Share:
23,152

Related videos on Youtube

Aminadav Glickshtein
Author by

Aminadav Glickshtein

Started coding at the age of seven, with the ability to program as if it were a native language. Has in-depth knowledge of software development, cloud, big data, web, desktop, cybersecurity, blockchain, DevOps and mobile development. Latest innovation: Control V

Updated on July 01, 2021

Comments

  • Aminadav Glickshtein
    Aminadav Glickshtein almost 3 years

    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');
    
  • Jack Allan
    Jack Allan about 8 years
    Is there a way to do this for anonymous inner functions?
  • SGD
    SGD about 8 years
    For JSDoc you would need some form of reference, I guess it depends on how the anonymous function is used. Do you have an example?
  • Ben Creasy
    Ben Creasy almost 7 years
    Is this documented anywhere officially? Can't find it.
  • SGD
    SGD almost 7 years
    Not in full depth I'm afraid. Did you try tagging your inner function with @function many_prompts~inner
  • Lucretiel
    Lucretiel over 6 years
    Is there a way to do this if you're using arrow functions? For instance: many_prompts = count => prompt => ...
  • SGD
    SGD over 6 years
    I have never tried, theoretically it should work as it is semantically the same but the question is if the available parsers do support this already.
  • Minh Nghĩa
    Minh Nghĩa almost 4 years
    Is there any documentation for this?
  • lance.dolan
    lance.dolan over 3 years
    Yeah, is this standard or non standard?
  • Trevor
    Trevor over 3 years
    Works in VSCode
  • Pang
    Pang about 3 years
    promt seems misspelt.
  • Mike Harrison
    Mike Harrison about 3 years
    You also don't need the parentheses: @return {function: void} works
  • Aaron Campbell
    Aaron Campbell about 2 years
    This doesn't seem to be recognized by VS Code.
  • Aaron Campbell
    Aaron Campbell about 2 years
    This seems to work in VS Code, but doesn't pick up on the inner function documentation. :(