javascript 如何使用 JSDoc 记录函数返回的函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30012043/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 11:29:09  来源:igfitidea点击:

How to document a function returned by a function using JSDoc

javascriptjsdocjsdoc3

提问by Aminadav Glickshtein

I am using JSDoc for parameter documentation.

我正在使用 JSDoc 作为参数文档。

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

很清楚如何记录 的参数类型many_prompts,但是记录它返回的函数的正确方法是什么?

/**
 * @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');

采纳答案by SGD

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

回答by gforceg

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

回答by Aminadav Glickshtein

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