62

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');

4 Answers 4

29

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;
  }
Sign up to request clarification or add additional context in comments.

3 Comments

You also don't need the parentheses: @return {function: void} works
This seems to work in VS Code, but doesn't pick up on the inner function documentation. :(
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.
28

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

5 Comments

Is there any documentation for this?
Yeah, is this standard or non standard?
Works in VSCode
also works when I use tsc to generate .d.ts files
This doesn't work on my end
26

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

7 Comments

Is there a way to do this for anonymous inner functions?
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?
Is this documented anywhere officially? Can't find it.
Is there a way to do this if you're using arrow functions? For instance: many_prompts = count => prompt => ...
This doesn't seem to be recognized by VS Code.
|
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');

2 Comments

> @return {Function} It never returns Function. Your example returns void.
@extempl Thanks, that was wrong! I changed it to void.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.