JSDoc Documentation
repository·main·Indexed 42 days ago
https://github.com/jsdoc/jsdoc.github.ioOfficial user documentation for JSDoc, a JavaScript API documentation generator. Includes guides on using block and inline tags, command-line options, configuration files (JSON and CommonJS), and customizing the default template.
What's inside JSDoc
- This repository contains the source files for the JSDoc documentation. If you are looking to use JSDoc itself (installing, configuring, or calling its APIs), please visit the official documentation site at https://jsdoc.app/.
Use the @memberof tag to assign symbols to parents
mainThe
@memberoftag identifies a symbol (like a function, variable, or property) as a member of a parent symbol (such as a namespace or class).By default,
@memberofdocuments symbols as static members. To document inner or instance members, you can either use scoping punctuation in the namepath or use the@inneror@instancetags.Syntax:
@memberof <parentNamepath>@memberof! <parentNamepath>(The!version forces the symbol to belong to the specified parent, even if JSDoc detects a different parent.)
/** @namespace */ var Tools = {}; /** @memberof Tools */ var hammer = function() { }; Tools.hammer = hammer;Resolve conflicting @kind declarations
mainIf you provide multiple tags that imply different kinds (for example, using both
@moduleand@kind constant), JSDoc resolves the conflict by using the last tag encountered in the documentation block./** * This will show up as a constant because @kind constant comes last. * @module myModule * @kind constant */ /** * This will show up as a module because @module comes last. * @kind constant * @module myModule */Use the @abstract tag to define abstract members
mainThe
@abstracttag is used to identify members (methods or properties) that are intended to be implemented or overridden by any object that inherits from the parent class. It serves as a documentation signal that the base implementation is incomplete or serves only as a template, often throwing an error if called directly./** * Parent class with abstract method */ function Parent() {} /** * @abstract * @return {boolean} */ Parent.prototype.method = function() { throw new Error('must be implemented by subclass!'); }; /** * Child class that implements the method * @augments Parent */ function Child() {} /** * @return {boolean} */ Child.prototype.method = function() { return true; };Define interfaces using virtual comments
mainIf you want to document an interface without writing actual implementation code, you can use "virtual comments." This allows you to define the structure of an interface (its methods and properties) purely through JSDoc annotations.
To do this:
- Use
@interface [Name]on a comment block to declare the interface. - Use
@functionand@name [InterfaceName]#[memberName]to define the members of that virtual interface.
/** * Interface for classes that represent a color. * * @interface Color */ /** * Get the color as an array of red, green, and blue values. * * @function * @name Color#rgb * @returns {Array<number>} An array containing the red, green, and blue values. */- Use
Use the @constructs tag to document class constructors
mainThe
@constructstag is used when defining a class via an object literal (often in conjunction with the@lendstag) to specify which function serves as the constructor for that class.If you are using
@lendsto link an object literal to a class, you can use@constructswithout an argument. If you are not using@lends, you must provide the name of the class as an argument to the tag (e.g.,@constructs ClassName).// Example with @lends (no name required) var Person = makeClass( /** @lends Person.prototype */ { /** @constructs */ initialize: function(name) { this.name = name; } } ); // Example without @lends (name required) makeClass('Menu', /** * @constructs Menu */ function (items) { }, {} );How module namepaths are determined for symbols
mainJSDoc uses specific namepath conventions to distinguish between module-private (inner) members and exported (static) members.
Inner vs. Static Members
- Inner Members: Variables or functions that are not exported are treated as module-private. They use the tilde (
~) separator. Example:module:myModule~foo. - Static Members: Symbols exported via
module.exports,exports, orthisare treated as static members of the module. They use the dot (.) separator. Example:module:myModule.bar.
Export Patterns
JSDoc automatically infers static membership when symbols are assigned to:
module.exportsexportsthis(common in constructor functions/classes)
/** @module myModule */ /** will be module:myModule~foo (inner) */ var foo = 1; /** will be module:myModule.bar (static) */ var bar = function() {};- Inner Members: Variables or functions that are not exported are treated as module-private. They use the tilde (
How to use namepaths to reference JavaScript variables
mainA namepath is a unique identifier used in JSDoc to refer to a JavaScript variable defined elsewhere in your documentation. Namepaths allow you to disambiguate between instance members, static members, and inner variables by using specific connecting symbols.
Namepath Syntax
- Instance members: Use
#(e.g.,Constructor#member) - Static members: Use
.(e.g.,Constructor.member) - Inner members: Use
~(e.g.,Constructor~member)
Chaining Namepaths
You can chain namepaths together using any combination of
#,., or~to reach deeply nested members.Example: To refer to a method
considerinside an instance memberIdeaof the constructorPerson, usePerson#Idea#consider./** @constructor */ Person = function() { /** @constructor */ this.Idea = function() { this.consider = function(){ return "hmmm"; } } } // Reference: Person#Idea#consider- Instance members: Use
Automatic export recognition in CommonJS and Node.js
mainJSDoc automatically recognizes exports in standard Node.js and CommonJS environments. You do not need to use the
@exportstag if you are using theexportsobject or themodule.exportsproperty./** * A module that says hello! * @module hello/world */ /** Say hello. */ exports.sayHello = function() { return 'Hello world'; };Omit symbols from documentation using `@ignore`
mainUse the
@ignoretag to prevent a symbol from appearing in your generated documentation. This tag takes precedence over all other JSDoc tags.Behavior by type:
@classor@module: Applying@ignoreto a class or module will omit the entire entity from the documentation.@namespace: If you ignore a namespace, you must also apply the@ignoretag to all child classes and namespaces within it. If you do not, the children will still appear in the documentation, but they will have incomplete/broken names.
/** * @class * @ignore */ function Jacket() { /** The jacket's color. */ this.color = null; }Authoring JSDoc 3 Plugins
mainJSDoc 3 plugins allow you to control the parsing process through three primary mechanisms:
- Event Handlers: Register handlers for specific named events fired by JSDoc (e.g.,
newDoclet,symbolFound). - Tag Definitions: Add new tags to the JSDoc tag dictionary to modify doclets when specific tags are encountered in comments.
- Node Visitors: Process every node in the Abstract Syntax Tree (AST) to document undocumented code or modify comments.
- Event Handlers: Register handlers for specific named events fired by JSDoc (e.g.,
Reference modules using the module: prefix
mainWhen using a namepath to refer to a module from a different JSDoc comment, you must prefix the module name with
module:.For example, to link to the
my/shirtmodule from themy/pantsmodule using the@seetag, usemodule:my/shirt.This prefix also applies to the namepaths of members within a module. If a module
my/pantsexports a classJeanswith an instance methodhem, the full namepath for that method ismodule:my/pants.Jeans#hem./** * Pants module. * @module my/pants * @see module:my/shirt */