How to document a function returned by a function using JSDoc
23,152
Solution 1
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;
}
Solution 2
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;
}
Solution 3
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;
}
Related videos on Youtube
Author by
Aminadav Glickshtein
Started coding at the age of seven, with the ability to program as if it were a native language. Has in-depth knowledge of software development, cloud, big data, web, desktop, cybersecurity, blockchain, DevOps and mobile development. Latest innovation: Control V
Updated on July 01, 2021Comments
-
Aminadav Glickshtein almost 3 years
I am using JSDoc for parameter documentation.
It is clear how to document the parameter types for
many_prompts
, but what is the right way to document the function it returns?/** * @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');
-
Jack Allan about 8 yearsIs there a way to do this for anonymous inner functions?
-
SGD about 8 yearsFor JSDoc you would need some form of reference, I guess it depends on how the anonymous function is used. Do you have an example?
-
Ben Creasy almost 7 yearsIs this documented anywhere officially? Can't find it.
-
SGD almost 7 yearsNot in full depth I'm afraid. Did you try tagging your inner function with
@function many_prompts~inner
-
Lucretiel over 6 yearsIs there a way to do this if you're using arrow functions? For instance:
many_prompts = count => prompt => ...
-
SGD over 6 yearsI have never tried, theoretically it should work as it is semantically the same but the question is if the available parsers do support this already.
-
Minh Nghĩa almost 4 yearsIs there any documentation for this?
-
lance.dolan over 3 yearsYeah, is this standard or non standard?
-
Trevor over 3 yearsWorks in VSCode
-
Pang about 3 years
promt
seems misspelt. -
Mike Harrison about 3 yearsYou also don't need the parentheses:
@return {function: void}
works -
Aaron Campbell about 2 yearsThis doesn't seem to be recognized by VS Code.
-
Aaron Campbell about 2 yearsThis seems to work in VS Code, but doesn't pick up on the inner function documentation. :(