有没有办法记录函数参数的参数?
Is there a way to document parameters of a function parameter?
假设你有这个功能:
/**
* 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');
}
正如@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 结果:
假设你有这个功能:
/**
* 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');
}
正如@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 结果: