有没有办法记录函数参数的参数?

时间:2018-03-11 17:42:21

标签: javascript jsdoc jsdoc3

说你有这个功能:

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

1 个答案:

答案 0 :(得分:2)

正如@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结果:

JSDoc result