简体   繁体   中英

Is there a convention for specifying the author, date, or source of code in JavaScript?

I would like to specify the sources of foreign functions (for example here from StackOverflow ). Also I would like to add my name, date and website to my own functions.

I can of course just put everything in a comment the way I want. But are there certain conventions I should follow? Maybe even machine readable?

JSDoc is usually used to specify metadata for javascript code, and can be used to automatically generate documentation. See: https://jsdoc.app/about-getting-started.html

Example:

/**
 * @author Some Guy <example@example.com>
 * @see {@link https://jsdoc.app/tags-description.html} for further information.
 * @description How to use JSDoc to tag javascript.
 */

There is no official way to specify this. At least no One True Way.

With that said, the most widely accepted is JSDoc comments . You need to start a block comment with /** (two asterisks) and then you can use the special JSDoc syntax to explain your source with @author and @see :

/**
 * Function that returns a random number
 * @author Jon Skeet
 * @see {@link https://stackoverflow.com/a/11373465}
 * 
 * @param {number} min - minimum bound (inclusive)
 * @param {number} max - maximum bound (inclusive)
 * @return {string} - uniformly distributed integer within the range as a string 
 */
function rand(min, max){
   return (Math.floor(Math.random() * (max - min + 1)) + min).toFixed(0);
}

Maybe even machine readable?

JSDoc is machine readable. There are many tools that consume it. Most notably, many standard JavaScript editors like Visual Studio Code will give you the JSDoc for a function on hover. Screenshot of code editor which shows that the JSDoc comment is shown when hovering over the function.

However, there are other tools that can consume JSDoc or subset of it. They may produce documentation, or perform type checking based on what is specified for @param and @return , or other tasks.

It is also worth noting that you do not need to have any tool in order to use JSDoc. You can write it directly in your source code even if it does not get consumed ever. It can still be read by other humans who would understand it. Even if it is the first time they encounter the documentation style, it is straight-forward enough to be easily understood.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM