Home > OS >  How can I add a description to a function in javascript?
How can I add a description to a function in javascript?

Time:08-04

I would like to add a description to a js function like I do it in C, C , Python ...

I do this by adding a comment at the top of the definition of the function, in the mentioned languages, but if I use JavaScript (pure, NodeJS, ReactJS) it does not show it. Ej:

Adding a Description in C

Result (When I put the cursor in a call of the function or definition): Description being shown

But I this behaviour does not replicate with Js/NodeJs/React. Just in case I'm using Visual Studio Code.

CodePudding user response:

VSCode, like most IDEs, will automatically process VSCode IDE showing the nifty function above being used, and showing a documentation box with the doc comments in it

You can augment that with types if you want type hints:

/**
 * Does something nifty.
 *
 * @param   {number} whatsit  The whatsit to use (or whatever).
 * @returns {string} A useful value.
 */
function nifty(whatsit) {
    return /*...*/;
}

If you use TypeScript, the types would be part of the code rather than in the JSDoc:

// TypeScript example
/**
 * Does something nifty.
 *
 * @param   whatsit  The whatsit to use (or whatever).
 * @returns A useful value.
 */
function nifty(whatsit: number): string {
    return /*...*/;
}
  • Related