[英]How to document a function returned by a function using JSDoc
I am using JSDoc for parameter documentation.我正在使用 JSDoc 作为参数文档。
It is clear how to document the parameter types for many_prompts
, but what is the right way to document the function it returns?很清楚如何记录many_prompts
的参数类型,但是记录它返回的 function 的正确方法是什么?
/**
* @param {Number} - number of times to prompt
* @return {Function(prompt{Number})} - the returned function
*/
function many_prompts(count) {
return function(prompt) {
for(var i=0; i < count; i++) alert(prompt);
}
}
//Example of use:
var y =many_prompts(3);
y('Hello World');
You can document the inner function and then reference it like so您可以记录内部函数,然后像这样引用它
/**
* @param {Number} - number of times to prompt
* @return {many_prompts~inner} - the returned function
*/
function many_prompts(count){
/**
* My inner function
*
* @param {object} prompt Some parameter
*/
var inner = function(prompt){
for(var i=0;i<count;i++) alert(prompt)
};
return inner;
}
This seems to be working for me.这似乎对我有用。
/**
* @param {Number} count - number of times to prompt
* @return {function(): void} - the returned function
*/
manyPrompts(count) {
/**
* My inner function
*
* @param {object} prompt Some parameter
*/
const inner = function(prompt) {
for (let i=0; i < count; i++) {
alert(prompt);
};
};
return inner;
}
The way I prefer:我喜欢的方式:
/**
* @param {number} count - number of times to prompt
* @returns { (promt:string) => void } - the returned function
*/
manyPrompts(count) {
/**
* My inner function
*
* @param {object} prompt Some parameter
*/
const inner = function(prompt) {
for (let i=0; i < count; i++) {
alert(prompt);
};
};
return inner;
}
This is my solution for this.这是我的解决方案。 I'm not describing a return in the first function and also document the inner function, which results in getting the documentation from the inner function.我没有描述第一个 function 中的返回,还记录了内部 function,这导致从内部 function 获取文档。
/**
* Function to create a Function with multiple prompt messages
* @function
* @param {Number} count - number of times to prompt
*/
function many_prompts(count) {
/**
* To prompt a value multiple times
* @function
* @param {String} prompt - parameter to prompt
* @return {Function} prompt the input parameter
*/
return function(prompt) {
for(var i=0; i < count; i++) alert(prompt);
}
}
//Example of use:
var y = many_prompts(3);
y('Hello World');
Which is then shown eg in vscode like this...然后像这样在 vscode 中显示...
For the outer function:对于外部 function:
For the inner function:对于内部 function:
You can also add an additional description when assigning the function, to describe the difference您也可以在分配function时添加额外的描述,描述差异
/**
* Function to prompt a value 3 times
* @function
* @param {Number} prompt - parameter to prompt
* @return {Function} prompt the input parameter
*/
const y = many_prompts(3);
y('Hello World');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.