How to document a function returned by a function using JSDoc

断了今生、忘了曾经 提交于 2019-11-27 17:16:52

问题


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

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



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


来源:https://stackoverflow.com/questions/30012043/how-to-document-a-function-returned-by-a-function-using-jsdoc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!