jsDoc學習記錄
一、安裝
npm install -g jsdoc # 全局安裝
二、書寫規範
1. 常用
@file
@fileoverview
@overview
文件開頭提供文件說明@summary
總結@author
作者@copyright
版權@version
版本@license
軟件許可協議@description
@desc
描述
/**
*@file 這是文件描述
*@summary 描述的簡要概括
*@author 作者
*@copyright 版權信息
*@version 版本
*@license 許可證
*/
/**
* 在開頭可以省略@description,直接寫描述
* @description 後面追加的描述會覆蓋前文
*/
@example
添加示例代碼
/**
* @example <caption>Usage Example</caption>
* var h = sayHello();
* console.log(h);
*/
function sayHello () {
return 'Hello';
}
@access
訪問權限,常用以下縮寫@private
@protected
@package
@public
/** constructor */
function Thingy() {
/** @private */
var foo = 0;
/** @public */
this.bar = true;
/** @protected */
this._inner = 'OK';
}
@param
@arg
@argument
函數參數的註釋
/**
* @param {string} someBody - someone you want to say hello to. // 普通寫法
* @param {string} someBody='John' - someone you want to say hello to. // 帶默認值
* @param {string} [someBody] - someone you want to say hello to. // 可選參數
* @param {number|string} someBody - someone you want to say hello to. // 多類型
* @param {*} someBody - someone you want to say hello to. // 任意類型
* @param {...string} someBody - someone you want to say hello to. // 可重複
*/
function sayHello (someBody) {
return 'Hello ' + someBody;
}
@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
}
}
};
@type
null
undefined
number
string
boolean
symbol
?number
可以是null
和number
!number
非空number
@typdef
自定義類型
/**
*@typedef {(number|string)} NumberLike
*/
@returns
@return
返回值
/**
*@returns {number}
*/
@throws
@exception
有可能的異常
/**
*@throws {InvalidTypeException}
*/
@class
@constructor
構造函數@constant
@const
常量@default
@defaultvalue
默認值
2. 其他
@alias
別名@abstract
抽象方法@virtual
@async
異步@arguments
@extends
繼承@borrows
借用已有文檔@lends
@callback
回調@deprecated
棄用@enum
枚舉變量集合@exports
導出@external
@host
外部@function
@func
@method
函數類型@readonly
只讀@requires
依賴@generator
生成器類型@yields
@yield
@global
全局@hideconstructor
隱藏構造函數@ignore
忽略註釋,註釋不生成Doc@interface
接口@implements
接口實現@inheritdoc
繼承文檔@inner
內部對象,內部成員Parent~Child
@instance
實例Parent#Child
@kind
種類,常用以下縮寫@class
@constant
@event
@external
@file
@function
@member
@mixin
@module
@namespace
@typedef
@listens
@member
@var
成員@memberof
@static
靜態成員@mixes
被混入@mixin
混入@namespace
@override
三、生成文檔
jsdoc yourFile.js