使用jsdoc记录回调的正确方法是什么?

rde*_*ges 58 javascript node.js jsdoc autodoc jsdoc3

我花了很长时间在互联网上寻找使用jsdoc正确记录回调的最佳方法,但遗憾的是,我还没有找到一个好的.

这是我的问题:

我正在为开发人员编写Node.js库.该库提供了开发人员将使用的多个类,函数和方法.

为了使我的代码清晰易懂,以及(希望)将来自动生成一些API文档,我已经开始在我的代码中使用jsdoc来自我记录正在发生的事情.

假设我定义了如下函数:

function addStuff(x, y, callback) {
  callback(x+y);
});
Run Code Online (Sandbox Code Playgroud)

使用jsdoc,我目前正在记录此函数,如下所示:

/**
  * Add two numbers together, then pass the results to a callback function.
  *
  * @function addStuff
  * @param {int} x - An integer.
  * @param {int} y - An integer.
  * @param {function} callback - A callback to run whose signature is (sum), where
  *  sum is an integer.
  */
function addStuff(x, y, callback) {
  callback(x+y);
});
Run Code Online (Sandbox Code Playgroud)

我觉得上面的解决方案有点像hack-ish,因为我无法用绝对术语来指定回调函数应该接受什么.

理想情况下,我想做的事情如下:

/**
  * Add two numbers together, then pass the results to a callback function.
  *
  * @function addStuff
  * @param {int} x - An integer.
  * @param {int} y - An integer.
  * @param {callback} callback - A callback to run.
  * @param {int} callback.sum - An integer.
  */
function addStuff(x, y, callback) {
  callback(x+y);
});
Run Code Online (Sandbox Code Playgroud)

以上看起来似乎允许我更简单地传达我的回调需要接受的内容.那有意义吗?

我想我的问题很简单:用jsdoc清楚地记录我的回调函数的最佳方法是什么?

感谢您的时间.

Jef*_*ams 78

JSDoc 3具有@callback标记,正是出于此目的.这是一个用法示例:

/**
 * Callback for adding two numbers.
 *
 * @callback addStuffCallback
 * @param {int} sum - An integer.
 */

/**
 * Add two numbers together, then pass the results to a callback function.
 *
 * @param {int} x - An integer.
 * @param {int} y - An integer.
 * @param {addStuffCallback} callback - A callback to run.
 */
function addStuff(x, y, callback) {
  callback(x+y);
}
Run Code Online (Sandbox Code Playgroud)

  • Visual Studio IDE不支持此功能.这里的另一个答案,`@ param {function(int):void} callback`在VS中工作,也更简洁. (12认同)

Yak*_*ein 29

另一种可能性是以这种方式描述传递给回调的值:

/**
  * Add two numbers together, then pass the results to a callback          function.
  *
  * @function addStuff
  * @param {int} x - An integer.
  * @param {int} y - An integer.
  * @param {function(int)} callback - A callback to run whose signature is (sum), where
  *  sum is an integer.
  */
function addStuff(x, y, callback) {
    callback(x+y);
});
Run Code Online (Sandbox Code Playgroud)

要记录回调的返回类型,请使用@param {function(int):string}.

  • @Maslow`function(int):string`(作为示例)在Visual Studio Code中适用于我. (4认同)
  • 我喜欢这种方法,因为我的 IDE (Netbeans) 支持它但似乎无法识别 @callback 标签 (2认同)
  • 包含 `function(int)` 的返回值怎么样? (2认同)

cod*_* đờ 11

@param {function(number):void} myCallback
Run Code Online (Sandbox Code Playgroud)

自 2021 年起在 VS Code 和 WebStorm 中运行良好


Vit*_*.us 8

使 VSCode 理解它的解决方法

/**
 * @typedef {function(FpsInfo)} fpsCallback
 * @callback fpsCallback
 * @param {FpsInfo} fps Fps info object
 */

 /**
 * @typedef {Object} FpsInfo
 * @property {number} fps The calculated frames per second
 * @property {number} jitter The absolute difference since the last calculated fps
 * @property {number} elapsed Milliseconds ellapsed since the last computation
 * @property {number} frames Number of frames since the last computation
 * @property {number} trigger Next computation will happen at this amount of frames
 */

/**
 * FPS Meter - Returns a function that is used to compute the framerate without the overhead of updating the DOM every frame.
 * @param {fpsCallback} callback Callback fired every time the FPS is computed
 * @param {number} [refreshRate=1] Refresh rate which the fps is computed and the callback is fired (0 to compute every frame, not recommended)
 * @returns {function} Returns a function that should be called on every the loop tick
 * @author Victor B - www.vitim.us - github.com/victornpb/fpsMeter
 */
function createFpsMeter(callback, refreshRate = 1) {
    // ...
}
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明 在此处输入图片说明