编程语言/JavaScript 注释规则:修订间差异
来自康健生活
创建页面,内容为“== 引言 ==” |
|||
| (未显示同一用户的26个中间版本) | |||
| 第1行: | 第1行: | ||
== 引言 == | == 引言 == | ||
=== 常用 === | |||
== 文件注释 == | |||
注释包括以下信息: | |||
* 概要说明及版本 | |||
* 项目地址 | |||
* 版权声明 | |||
* 开源协议 | |||
* 版本号 | |||
* 修改时间(ISO 格式) | |||
<syntaxhighlight lang="javascript"> | |||
/*! | |||
* ... | |||
*/</syntaxhighlight> | |||
=== eq === | |||
<syntaxhighlight lang="javascript"> | |||
/** | |||
* @file webpack 配置文件 | |||
* @author kangkk(kangjian#360.cn) | |||
* @date 2021 | |||
* @version 1.0.0 | |||
* @see {@link https://webpack.js.org/configuration|webpack configuration} | |||
* @LastModifiedBy kangkk | |||
* @LastModifiedTime 2022-03-21 17:17:26 | |||
*/</syntaxhighlight> | |||
== 普通注释 == | |||
=== // === | |||
<syntaxhighlight lang="javascript"> | |||
// this is comment | |||
// ? 解释为什么实现 | |||
// ! 这里有一个错误 | |||
// * 这只是注释 | |||
// // 这是一行被删除的注释 | |||
</syntaxhighlight> | |||
==== TODO ==== | |||
功能未完成,待完善。某段代码有功能未实现,或者有待完善,必须添加 <code>TODO</code> 标记 | |||
<syntaxhighlight lang="javascript"> | |||
// TODO ...</syntaxhighlight> | |||
==== FIXME ==== | |||
待修复。某段代码有功能运行没问题,但可能由于时间赶或者其他原因,需要修正。此时需要对如何修正进行简单说明 | |||
<syntaxhighlight lang="javascript"> | |||
// FIXME ...</syntaxhighlight> | |||
==== HACK ==== | |||
待优化。为修正某些问题而写的不太好或者使用了某些诡异手段的代码 | |||
<syntaxhighlight lang="javascript"> | |||
// HACK ...</syntaxhighlight> | |||
==== XXX ==== | |||
实现方法待确认。虽然实现了功能,但是实现的方法有待商榷 | |||
<syntaxhighlight lang="javascript"> | |||
// XXX ...</syntaxhighlight> | |||
==== NOTE ==== | |||
代码功能说明。说明代码如何工作 | |||
<syntaxhighlight lang="javascript"> | |||
// NOTE ...</syntaxhighlight> | |||
==== BUG ==== | |||
有 Bug | |||
<syntaxhighlight lang="javascript"> | |||
// BUG ...</syntaxhighlight> | |||
=== /* */ === | |||
<syntaxhighlight lang="javascript"> | |||
/* | |||
... | |||
*/ | |||
</syntaxhighlight> | |||
== 类型定义 == | |||
{| class="wikitable" | |||
|- | |||
! name !! eq !! description | |||
|- | |||
| String || {string} || -- | |||
|- | |||
| Number || {number} || -- | |||
|- | |||
| Boolean || {boolean} || -- | |||
|- | |||
| Object || {Object} || -- | |||
|- | |||
| Function || {Function} || -- | |||
|- | |||
| RegExp || {RegExp} || -- | |||
|- | |||
| Array || {Array} || -- | |||
|- | |||
| Date || {Date} || -- | |||
|- | |||
| 单一类型集合 || <nowiki>{Array.<string>}</nowiki> || string 类型的数组 | |||
|- | |||
| 多类型 || <nowiki>{(number|boolean)}</nowiki> || 可能是 number 类型, 也可能是 boolean 类型 | |||
|- | |||
| 允许为null || <nowiki>{?number}</nowiki> || 可能是 number, 也可能是 null | |||
|- | |||
| 不允许为null || <nowiki>{!Object}</nowiki> || Object 类型, 但不是 null | |||
|- | |||
| Function类型 || <nowiki>{function(number, boolean)}</nowiki> || 函数, 形参类型 | |||
|- | |||
| Function带返回值 || <nowiki>{function(number, boolean):string}</nowiki> || 函数, 形参, 返回值类型 | |||
|- | |||
| 参数可选 || <nowiki>@param {string=} name</nowiki> || 可选参数, =为类型后缀 | |||
|- | |||
| 可变参数 || <nowiki>@param {...number} args</nowiki> || 变长参数, ...为类型前缀 | |||
|- | |||
| 任意类型 || <nowiki>{*}</nowiki> || 任意类型 | |||
|- | |||
| 可选任意类型 || <nowiki>@param {*=} name</nowiki> || 可选参数,类型不限 | |||
|- | |||
| 可变任意类型 || <nowiki>@param {...*} args</nowiki> || 变长参数,类型不限 | |||
|} | |||
=== 组合 === | |||
{| class="wikitable" | |||
|- | |||
! name !! eq !! description | |||
|- | |||
| <nowiki>String|Null|Undefined</nowiki> || <nowiki>@param {string|null|undefined} str</nowiki> || -- | |||
|- | |||
| <nowiki>String|Null|Undefined</nowiki> || <nowiki>@param {?string|undefined} str</nowiki> || -- | |||
|- | |||
| 所有可变参数都是数字 || <nowiki>@param {...number} num</nowiki> || -- | |||
|- | |||
| 表示一个参数可以接受多种类型 || <nowiki>@param {(string|string[])} [somebody=John Doe]</nowiki> || -- | |||
|- | |||
| 非空数组 || <nowiki>@param {!Array} arr</nowiki> || -- | |||
|- | |||
| 非空数组或 undefined || <nowiki>@param {!Array|undefined} arr</nowiki> || -- | |||
|- | |||
| 不定数量参数 || <nowiki>@param {...} purchases</nowiki> || -- | |||
|- | |||
| 函数类型值的可选参数 || <nowiki>@param {function(string, string=)}</nowiki> || -- | |||
|- | |||
| 函数类型值的可变参数 || <nowiki>@param {function(string, ...[number]): number}</nowiki> || -- | |||
|} | |||
=== eq === | |||
<syntaxhighlight lang="javascript" highlight="6"> | |||
/** | |||
* @func | |||
* @desc 一个带参数的函数 | |||
* @param {string} a - 参数a | |||
* @param {number} b=1 - 参数b默认值为1 | |||
* @param {string} c=1 - 参数c有两种支持的取值 1—表示x 2—表示xx | |||
* @param {object} d - 参数d为一个对象 | |||
* @param {string} d.e - 参数d的e属性 | |||
* @param {object[]} g - 参数g为一个对象数组 | |||
* @param {string} g.h - 参数g数组中一项的h属性 | |||
* @param {string} [j] - 参数j是一个可选参数 | |||
*/ | |||
function foo(a, b, c, d, g, j) {} | |||
/** | |||
* @func | |||
* @desc 一个带若干参数的函数 | |||
* @param {...string} a - 参数a | |||
*/ | |||
function bar(a) {}</syntaxhighlight> | |||
== 文档注释 == | |||
<syntaxhighlight lang="javascript"> | |||
/** | |||
* ... | |||
*/ | |||
</syntaxhighlight> | |||
=== 可选参数 === | |||
{| class="wikitable" | |||
|- | |||
! name !! description !! alias !! eq | |||
|- | |||
| @ || 标签 || || <syntaxhighlight lang="javascript"> | |||
/** | |||
* @param | |||
*/</syntaxhighlight> | |||
|- | |||
| - || 说明文字 || || <syntaxhighlight lang="javascript"> | |||
/** | |||
* @param {string} name - 名字 | |||
*/</syntaxhighlight> | |||
|- | |||
| @for || 所属类名 || || | |||
|- | |||
| @abstract || 成员必须在继承类中实现(或重写) || @virtual || 示例 | |||
|- | |||
| @access || 指定该成员的访问级别(私有 private,公共 public,或保护 protected) || || 示例 | |||
|- | |||
| @alias || 标记成员有一个别名 || || 示例 | |||
|- | |||
| @augments || 指名这个子类继承至哪个父类,后面需要加父类名 || @extends || 示例 | |||
|- | |||
| @author || 指定项目的作者 || || 示例 | |||
|- | |||
| @borrows || 允许将另一个标识符的描述添加到你的当前描述 || || 示例 | |||
|- | |||
| @callback || 描述回调函数 || || <syntaxhighlight lang="javascript"> | |||
/** | |||
* @function toDragOver | |||
* @description - | |||
* @param {Event} _ - | |||
* @param {string} info.nodeName - 节点标签名称 | |||
* @param {Element} info.node - 被拖拽的节点 | |||
* @param {toDragOver~callback} cb - 回调函数 | |||
* @returns {null} | |||
*/ | |||
/** | |||
* @callback toDragOver~callback | |||
* @param {Element} target - 目标元素 | |||
* @param {number} parentId - 目标元素是未打开的父级元素时,父级元素的 id | |||
*/ | |||
export const toDragOver = (_, info, cb) => { | |||
... | |||
cb(void 0, id); | |||
}</syntaxhighlight> | |||
|- | |||
| @class || 标明函数是一个构造器函数 || @constructor || @class 必须搭配 @constructor 或 @static 使用 | |||
|- | |||
| @classdesc || 使用后面的文字来描述整个类 || || 示例 | |||
|- | |||
| @constant || 记录一个对象作为一个常量 || @const || 示例 | |||
|- | |||
| @constructs || 这个函数成员将成为类的构造函数 || || 示例 | |||
|- | |||
| @copyright || 描述一些版权信息 || || 示例 | |||
|- | |||
| @default || 记录默认值 || @defaultvalue || 示例 | |||
|- | |||
| @deprecated || 说明这已不再是首选方法 || || 示例 | |||
|- | |||
| @description || 描述一个标识 || @desc || 示例 | |||
|- | |||
| @enum || 描述一个相关属性的集合 || || 示例 | |||
|- | |||
| @event || 描述一个事件 || || 示例 | |||
|- | |||
| @example || 提供一个如何使用描述项的例子 || || 示例 | |||
|- | |||
| @exports || 标识一个由JavaScript模块导出的成员 || || 示例 | |||
|- | |||
| @external || 标识一个外部的类,命名空间,或模块 || @host || 示例 | |||
|- | |||
| @file || 描述一个文件 || @fileoverview/@overview || 示例 | |||
|- | |||
| @fires || 描述事件这个方法可能会触发 || @emits || 示例 | |||
|- | |||
| @function || 描述一个函数或方法 || @func/@method || 示例 | |||
|- | |||
| @global || 记录一个全局对象 || || 示例 | |||
|- | |||
| @ignore || 忽略文档中的一个标识 || || 示例 | |||
|- | |||
| @implements || 标识实现一个接口 || || 示例 | |||
|- | |||
| @inheritdoc || 指明这个标识应继承其父类的文档 || || 示例 | |||
|- | |||
| @inner || 描述一个内部对象 || || 示例 | |||
|- | |||
| @instance || 记录一个实例成员 || || 示例 | |||
|- | |||
| @interface || 标签使一个标识符作为其他标识符的一个实现接口 || || 示例 | |||
|- | |||
| @kind || 标识的类型 || || 取值为: | |||
# class | |||
# constant | |||
# event | |||
# external | |||
# file | |||
# function | |||
# member | |||
# mixin | |||
# module | |||
# namespace | |||
# typedef | |||
|- | |||
| @lends || 将一个字面量对象的所有属性标记为某个标识符(类或模块)的成员 || || 示例 | |||
|- | |||
| @license || 标识你的代码采用何种软件许可协议 || || <syntaxhighlight lang="javascript"> | |||
/** | |||
* // @license <identifier> | |||
* @license Apache-2.0 | |||
* // | |||
* @license | |||
* Copyright (c) 2015 Example Co | |||
* ... | |||
* ER DEALINGS IN THE SOFTWARE. | |||
*/</syntaxhighlight> | |||
|- | |||
| @listens || 列出一个标识的监听事件 || || <syntaxhighlight lang="javascript"> | |||
/** | |||
* // @listens <event name> | |||
* @listens module:hurler~event:snowball | |||
*/</syntaxhighlight> | |||
|- | |||
| @member || 记录一个成员 || @var || <syntaxhighlight lang="javascript"> | |||
/** | |||
* // @member [<type>] [<name>] | |||
* @member {Object} | |||
*/</syntaxhighlight> | |||
|- | |||
| @memberof || 标明这个标识属于哪个父级标识 || || <syntaxhighlight lang="javascript"> | |||
/** | |||
* // @memberof <parent namepath> | |||
* // @memberof! <parent namepath> | |||
* @memberof Tools | |||
*/</syntaxhighlight> | |||
|- | |||
| @mixes || 此对象混入了另一个对象中的所有成员 || || <syntaxhighlight lang="javascript"> | |||
/** | |||
* // @mixes <OtherObjectPath> | |||
* @mixes Eventful | |||
*/</syntaxhighlight> | |||
|- | |||
| @mixin || 记录一个mixin(混入)对象 || || <syntaxhighlight lang="javascript"> | |||
/** | |||
* // @mixin [<MixinName>] | |||
* @mixin | |||
*/</syntaxhighlight> | |||
|- | |||
| @module || 记录一个 JavaScript 模块 || || <syntaxhighlight lang="javascript"> | |||
/** | |||
* // @module [[{<type>}] <moduleName>] | |||
* @module myModule | |||
*/</syntaxhighlight> | |||
|- | |||
| @name || 记录一个对象的名称 || || <syntaxhighlight lang="javascript"> | |||
/** | |||
* // @name <namePath> | |||
* @name highlightSearchTerm | |||
*/</syntaxhighlight> | |||
|- | |||
| @namespace || 记录一个命名空间对象 || || <syntaxhighlight lang="javascript"> | |||
/** | |||
* // @namespace [{<type>}] <Some name>] | |||
* @namespace MyNamespace | |||
*/</syntaxhighlight> | |||
|- | |||
| @override || 指明一个标识符覆盖其父类同名的标识符 || || <syntaxhighlight lang="javascript"> | |||
/** | |||
* // @override | |||
* @override | |||
*/</syntaxhighlight> | |||
|- | |||
| @param || 声明函数参数 || @arg/@argument || <syntaxhighlight lang="javascript"> | |||
/** | |||
* @param {string} a - 必传参数 | |||
* @param {number} [b=2] - 参数有默认值 | |||
* @param {number} [c] - 可选参数 | |||
*/ | |||
function test(a, b, c) {}</syntaxhighlight> | |||
|- | |||
| @property || 记录一个对象的属性 || @prop || <syntaxhighlight lang="javascript"> | |||
/** | |||
* // @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 | |||
} | |||
} | |||
};</syntaxhighlight> | |||
|- | |||
| @private || 标记为私有 || || <syntaxhighlight lang="javascript"> | |||
/** | |||
* // @private | |||
* @private | |||
*/</syntaxhighlight> | |||
|- | |||
| @protected || 标记为受保护 || || <syntaxhighlight lang="javascript"> | |||
/** | |||
* // @protected | |||
* @protected | |||
*/</syntaxhighlight> | |||
|- | |||
| @public || 标记为公开 || || <syntaxhighlight lang="javascript"> | |||
/** | |||
* // @public | |||
* @public | |||
*/</syntaxhighlight> | |||
|- | |||
| @readonly || 标记为只读 || || <syntaxhighlight lang="javascript"> | |||
/** | |||
* // @readonly | |||
* @readonly | |||
*/</syntaxhighlight> | |||
|- | |||
| @requires || 记录这个文件需要一个 JavaScript 模块 || || <syntaxhighlight lang="javascript"> | |||
/** | |||
* // @requires <some module name> | |||
* @requires module:xyzcorp/helper | |||
* @requires xyzcorp/helper.ShinyWidget#polish | |||
*/</syntaxhighlight> | |||
|- | |||
| @returns || 记录一个函数的返回值 || @return || <syntaxhighlight lang="javascript"> | |||
/** | |||
* // @returns | |||
* @returns {Number} Sum of a and b | |||
* @returns {Number|Array} Sum of a and b or an array that contains a, b and the sum of a and b. | |||
*/</syntaxhighlight> | |||
|- | |||
| @see || 更多详细信息 || || <syntaxhighlight lang="javascript"> | |||
/** | |||
* // @see <namepath> | |||
* // @see <text> | |||
* @see {@link bar} | |||
* @see {@link http://github.com|GitHub} | |||
* @see bar | |||
*/</syntaxhighlight> | |||
|- | |||
| @since || 记录功能被添加版本节点 || || <syntaxhighlight lang="javascript"> | |||
/** | |||
* // @since <version description> | |||
* @since 1.0.0 | |||
*/</syntaxhighlight> | |||
|- | |||
| @static || 记录一个静态成员 || || <syntaxhighlight lang="javascript"> | |||
/** | |||
* // @static | |||
* @static | |||
*/</syntaxhighlight> | |||
|- | |||
| @summary || 描述的一个简写版本 || || <syntaxhighlight lang="javascript"> | |||
/** | |||
* // @summary Summary goes here. | |||
* @summary A concise summary. | |||
*/</syntaxhighlight> | |||
|- | |||
| @this || 关键字的指向 || || <syntaxhighlight lang="javascript"> | |||
/** | |||
* // @this <namePath> | |||
* @this Greeter | |||
*/</syntaxhighlight> | |||
|- | |||
| @throws || 说明可能会被抛出的错误 || @exception || <syntaxhighlight lang="javascript"> | |||
/** | |||
* // @throws free-form description | |||
* // @throws {<type>} | |||
* // @throws {<type>} free-form description | |||
* @throws {InvalidArgumentException} | |||
* @throws Will throw an error if the argument is null. | |||
* @throws {DivideByZero} Argument x must be non-zero. | |||
*/</syntaxhighlight> | |||
|- | |||
| @todo || 记录一个将要完成的任务 || || <syntaxhighlight lang="javascript"> | |||
/** | |||
* // @todo text describing thing to do. | |||
* @todo Write the documentation. | |||
*/</syntaxhighlight> | |||
|- | |||
| @type || 记录一个对象的类型 || || <syntaxhighlight lang="javascript"> | |||
/** | |||
* // @type {type name} | |||
* @type {(string|Array.<string>)} | |||
* @type {number} | |||
*/</syntaxhighlight> | |||
|- | |||
| @typedef || 记录一个自定义的类型 || || <syntaxhighlight lang="javascript"> | |||
/** | |||
* // @typedef [<type>] <namepath> | |||
*/ | |||
// 定义了一个联合类型的参数,表示可以包含数字或字符串 | |||
/** | |||
* A number, or a string containing a number. | |||
* @typedef {(number|string)} NumberLike | |||
*/ | |||
/** | |||
* Set the magic number. | |||
* @param {NumberLike} x - The magic number. | |||
*/ | |||
function setMagicNumber(x) { | |||
} | |||
// 定义了一个更复杂的类型,一个对象的几个属性,并设置其namepath | |||
/** | |||
* The complete Triforce, or one or more components of the Triforce. | |||
* @typedef {Object} WishGranter~Triforce | |||
* @property {boolean} hasCourage - Indicates whether the Courage component is present. | |||
* @property {boolean} hasPower - Indicates whether the Power component is present. | |||
* @property {boolean} hasWisdom - Indicates whether the Wisdom component is present. | |||
*/ | |||
/** | |||
* A class for granting wishes, powered by the Triforce. | |||
* @class | |||
* @param {...WishGranter~Triforce} triforce - One to three {@link WishGranter~Triforce} objects | |||
* containing all three components of the Triforce. | |||
*/ | |||
function WishGranter() {}</syntaxhighlight> | |||
|- | |||
| @variation || 区分具有相同名称的不同的对象 || || <syntaxhighlight lang="javascript"> | |||
/** | |||
* // @variation <variation number> | |||
* @variation 6 | |||
*/</syntaxhighlight> | |||
|- | |||
| @version || 版本 || || <syntaxhighlight lang="javascript"> | |||
/** | |||
* // @version <version number> | |||
* @version 0.0.1 | |||
*/</syntaxhighlight> | |||
|} | |||
内链 | |||
{| class="wikitable" | |||
|- | |||
! name !! description !! alias !! eq | |||
|- | |||
| @link || 链接到文档中的另一个项目 || @linkcode/@linkplain || <syntaxhighlight lang="javascript"> | |||
/** | |||
* // {@link <namepath or URL>} | |||
* // [link text]{@link <namepath or URL>} | |||
* // {@link <namepath or URL>|<link text>} | |||
* // {@link <namepath or URL> <link text>} | |||
* See {@link MyClass} | |||
* See [MyClass's foo property]{@link MyClass#foo} | |||
* See {@link https://kangkk.com} | |||
* See {@link https://kangkk.com|kangkk} | |||
* See {@link https://kangkk.com kangkk} | |||
*/ | |||
# {@linkcode}:强制使用等宽字体链接文本。 | |||
# {@linkplain}:强制显示为正常的文本,没有等宽字体链接文本。</syntaxhighlight> | |||
|- | |||
| @tutorial || 链接到一个教程(创建一个链接到您指定的教程标识符) || || <syntaxhighlight lang="javascript"> | |||
/** | |||
* // {@tutorial <tutorial ID>} | |||
* // [link text]{@tutorial <tutorial ID>} | |||
* // {@tutorial <tutorial ID>|<link text>} | |||
* // {@tutorial <tutorial ID> <link text>} | |||
* See {@tutorial dashboard} | |||
* See [Configuring the Dashboard]{@tutorial dashboard} | |||
* See {@tutorial dashboard|Configuring the Dashboard} | |||
* See {@tutorial dashboard Configuring the Dashboard} | |||
*/</syntaxhighlight> | |||
|} | |||
=== 自定义 === | |||
{| class="wikitable" | |||
|- | |||
! name !! description !! other !! eq | |||
|- | |||
| @date || 创建时间 || || <syntaxhighlight lang="javascript"> | |||
/** | |||
* @date 2022-03-21 14:07:32 | |||
*/</syntaxhighlight> | |||
|- | |||
| @lastModifiedBy || 最后修改时间 || @lastEditors || <syntaxhighlight lang="javascript"> | |||
/** | |||
* @lastModifiedBy kangkk | |||
*/</syntaxhighlight> | |||
|- | |||
| @lastModifiedTime || 最后修改人 || @lastEditTime || <syntaxhighlight lang="javascript"> | |||
/** | |||
* @lastModifiedTime 2022-03-21 14:07:32 | |||
*/</syntaxhighlight> | |||
|- | |||
| @filePath || 文件相对工程路径 || || | |||
|} | |||