JSDoc 是什么?
JSDoc是Javascript源代码的标记语言(特殊格式的注释)。
JSDoc用于为JavaScript应用或库中的API生成文档。
即以特殊格式的注释来描述JavaScript应用或库中的API的作用、参数列表、返回值等特性,而后通过工具自动生成API文档。
JSDoc标签
JSDoc标签用于提供更多信息。
示例:
- @constructor表示构造函数
- @param表示参数
/*** 表示一本书* @constructor* @param {string} title - 书的标题* @param {string} author - 书的作者*/function Book(title, author) {}
标签实示例
来源标识
@author / @file / @version/*** @author Jane Smith <jsmith@example.com>* @file Manages the configuration settings for the widget.* @version 1.2.3*/function MyClass() {}
访问性
@abstract (@virtual) @access / @private / @protected / @packeg / @public / @global /@instance ```javascript /* @global / var foo = ‘hello foo’;
/ @namespace */ var BaseObject = { /
* foo is now BaseObject#foo rather than BaseObject.foo.* @instance 成员变量*/foo: null
};
/**
- Check whether the dairy product is solid at room temperature.
- @abstract
- @return {boolean} */ DairyProduct.prototype.isSolid = function() { throw new Error(‘must be implemented by subclass!’); };
/* @constructor / function Thingy() {
/** @access private */var foo = 0;/** @access protected */this._bar = 1;/** @access package */this.baz = 2;/** @access public */this.pez = 3;
}
// same as…
/* @constructor / function OtherThingy() {
/** @private */var foo = 0;/** @protected */this._bar = 1;/** @package */this.baz = 2;/** @public */this.pez = 3;
}
**@static**<br />两种用法```javascript/** @namespace MyNamespace *//*** @function myFunction* @memberof MyNamespace* @static*/
/** @module Rollerskate *//*** The 'wheel' variable is documented as Rollerskate.wheel* rather than Rollerskate~wheel.* @static*/var wheel = 1;
类型标识
@enum
/*** Enum for tri-state values.* @readonly* @enum {number}*/var triState = {/** The true value */TRUE: 1,FALSE: -1,/** @type {boolean} */MAYBE: true};
@property
/*** @namespace* @property {object} defaults - The default values for parties.* @property {number} defaults.players - The default number of players.* @property {string} defaults.level - The default level for the party.* @property {object} defaults.treasure - The default treasure.* @property {number} defaults.treasure.gold - How much gold the party starts with.*/var config = {defaults: {players: 1,level: 'beginner',treasure: {gold: 0}}};
@member [
/** @class */function Data() {/** @member {Object} */this.point = {};}
- @function
- @class
- @constant
- @constructs
- @generator
- @global
@callback
```javascript /**- @callback myCallback
- @param {number} x - … */
/* @type {myCallback} / var cb;
<a name="ZpLnQ"></a>## 不常用- [@async](https://jsdoc.app/tags-async.html) 标识异步函数- [@augments](https://jsdoc.app/tags-augments.html) 标识其他参数- [@ignore](https://jsdoc.app/tags-ignore.html)- [@kind](https://jsdoc.app/tags-kind.html)- [@type](https://jsdoc.app/tags-type.html) 类型表示案例<a name="S1AyP"></a>#<a name="Pp3xH"></a># 安装工具在注释写好之后,下面我们来安装jsdoc,它的作用是从源文件中将注释提取出来并生成文档。<br />注意:JSDoc是标记语言,jsdoc是工具。<a name="cfwWN"></a>## 安装```javascriptnpm install --save-dev jsdoc# npm install -g jsdoc //全局安装
生成文档
jsdoc book.js
参考:
- JSDoc 3 官方文档:https://jsdoc.app/
- 配置参考: https://jsdoc.zcopy.site/about-configuring-jsdoc.html
