【问题标题】:Is there a way to document parameters of a function parameter?有没有办法记录函数参数的参数?
【发布时间】:2018-08-19 18:09:57
【问题描述】:

假设你有这个功能:

/**
 * doSomething description
 * @param {function} fn - A function that accepts an argument.
 */
function doSomething( fn ) {
    fn.call(this, 'This is a test');
}

doSomething 应该这样使用:

doSomething( function( text ) {
    console.log( text );
});

我的问题是: JSDoc 中是否有正式的方法来记录 fn 参数? 可能是这样的:

/**
 * doSomething description
 * @param {function} fn - A function that accepts an argument.
 * @param {string} fn( name ) - A text passed to the fn function.
 */
function doSomething( fn ) {
    fn.call(this, 'test');
}

【问题讨论】:

  • 不,你不会在 JSDoc 中找到类似的东西。另一方面,Flow 和 TypeScript...
  • @SergiuParaschiv :非常感谢,但有什么解决方法吗?
  • 虽然有@callback,也许JSDoc认为这是一个回调对你来说没什么大不了的:) usejsdoc.org/tags-param.html#callback-functions - 这样至少你可以指定参数类型。
  • @SergiuParaschiv :在发布问题之前我已经看到了,我只是不喜欢它分离两个功能的方式。老实说,我以前从未听说过 Flow。我在 google 上查了一下,看起来很酷。谢谢你,我会开始学习 Flow 的。你太棒了:)
  • Flow 是 awesome :) 我自己已经切换到 TS,因为它除了严格的类型检查之外还带来了很多其他很酷的东西,但如果这就是你所需要的,那么 Flow 应该是足够了。祝你好运!

标签: javascript jsdoc jsdoc3


【解决方案1】:

正如@SergiuParaschiv 在他的评论中提到的那样,唯一的方法就是像这样使用@callback 标签:

Javascript 代码:

/**
 * A function to be passed as an argument.
 * @callback doSomethingCallback
 * @param {string} text - A simple text.
 */

 /**
  * doSomething description
  * @param {doSomethingCallback} fn - A function that accepts an argument.
  */
 function doSomething( fn ) {
     fn.call(this, 'This is a test');
 }

JSDoc 结果:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-26
    • 1970-01-01
    • 1970-01-01
    • 2019-09-23
    • 1970-01-01
    • 1970-01-01
    • 2013-03-01
    • 2019-06-02
    相关资源
    最近更新 更多