Javascript 是否有方法记录函数参数的参数?
假设你有这个功能:Javascript 是否有方法记录函数参数的参数?,javascript,jsdoc,jsdoc3,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( function( text ) { console.log( text ); });
/**
* doSomething description
* @param {function} fn - A function that accepts an argument.
*/
function doSomething( fn ) {
fn.call(this, 'This is a test');
}
剂量测定法应该这样使用:
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在他的评论中提到的,唯一的方法是使用这样的标签: 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结果:
/**
* 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中找不到类似的内容。另一方面,Flow和TypeScript…@SergiuParaschiv:非常感谢,但是有解决方法吗?虽然有
@callback
,但JSDoc认为它是回调对您来说可能没什么大不了的:)-这样至少可以指定参数类型。@SergiuParaschiv:在发布问题之前,我已经看到了,我只是不喜欢它把两个函数分开的方式。老实说,我以前从未听说过Flow。我在谷歌上查过它,它看起来很酷。多亏了你,我将开始学习Flow。你真棒:)流量真棒:)我自己也换成了TS,因为除了严格的类型检查之外,它还带来了很多其他很酷的东西,但如果这就是你所需要的,那么流量应该足够了。祝你好运!