资讯专栏INFORMATION COLUMN

JavaScript代码注释范例

int64 / 624人阅读

摘要:包含描述与指定所有参数和返回值的类型和值的注释标签。返回值的类型和描述或者更多示例更多请参考以下网站为本文参考,欢迎留言纠正。注解注释原文代码注释规范与示例注释

JavaScript代码注释范例
做为一个有情怀的Coder,最近收集了一下JavaScript代码注释范例,希望能够帮助大家撸得一手妖媚而又放荡的Bug。
普通注释 单行注释

使用 // 作为单行注释。

单行注释符后与注释内容保留一个空格。

//bad comments
// good comments

单行注释需要在说明的代码之上另起一行,并且在注释前插入空行。

function isType(content, expect) {

    // good
    // Using Object.prototype.toString to judge data types.
    let type = Object.prototype.toString.call(content).replace(/[objects|]/g, "");

    return type === expect;
}
// bad
console.log(isType("hello", "String"));       // return true

带有 // FIXME:// TODO: 的前缀的注释可以帮助其他开发者快速了解这是一个需要复查的问题,或是给需要实现的功能提供一个解决方式。这将有别于常见的注释,因为它们是可操作的。使用 FIXME -- need to figure this out 或者 TODO -- need to implement

使用 // FIXME: 标注问题。

class Calculator {
    constructor() {
        // FIXME: shouldn"t use a global here
        total = 0;
    }
}

使用 // TODO: 标注问题的解决方式。

// Support: IE, Opera, Webkit
// TODO: identify versions
// getElementById can match elements by name instead of ID
if ( newContext && (elem = newContext.getElementById( m )) &&
    contains( context, elem ) &&
    elem.id === m ) {

    results.push( elem );
    return results;
}
多行注释

多行注释星号 * 对齐,并且注释内容不要写在起始符号 /**与结束符号 */ 所在行。

// bad
/* matches from matchExpr["CHILD"]
  1 type (only|nth|...)
   2 what (child|of-type)
*/

// good
/* 
    matches from matchExpr["CHILD"]
    1 type (only|nth|...)
    2 what (child|of-type)
 */
文件注释(多行注释)

使用 /* ... */ 作为文件注释。

文件注释主要包含:概要介绍、版本信息、版权声明、开源协议、修改时间等说明内容。

React.js

/** @license React v16.4.0
 * react.development.js
 *
 * Copyright (c) 2013-present, Facebook, Inc.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

jQuery.js

/*!
 * jQuery JavaScript Library v3.3.1
 * https://jquery.com/
 *
 * Includes Sizzle.js
 * https://sizzlejs.com/
 *
 * Copyright JS Foundation and other contributors
 * Released under the MIT license
 * https://jquery.org/license
 *
 * Date: 2018-01-20T17:24Z
 */

开源项目的开发版本生产版本中都会保留文件注释,且必须对引用的其他开源代码进行说明。

文档注释(多行注释)

使用 /** ... */ 作为文档API注释。包含描述与指定所有参数和返回值的类型和值的注释标签

/**
 * Maps children that are typically specified as `props.children`.
 *
 * See https://reactjs.org/docs/react-api.html#react.children.map
 *
 * The provided mapFunction(child, key, index) will be called for each
 * leaf child.
 *
 * @param {?*} children Children tree container.
 * @param {function(*, int)} func The map function.
 * @param {*} context Context for mapFunction.
 * @return {object} Object containing the ordered map of results.
 */
function mapChildren(children, func, context) {

  if (children == null) {
    return children;
  }

  var result = [];
  mapIntoWithKeyPrefixInternal(children, result, null, func, context);

  return result;
}
常用注释标签

@param 指定参数的名称。您还可以包含参数的数据类型,使用大括号括起来,和参数的描述。

/**
 * @param content
 */

注释变量名 和 变量类型

/**
 * @param {String} type
 */

注释变量名、变量类型 和 变量说明

/**
 * @param {String} attrs Pipe-separated list of attributes
 */

连字符可以使注释阅读友好

参数变量名与参数说明之间使用连字符 -

/**
 * @param {object} partialState - Next partial state to be merged with state.
 */

对象参数属性描述

描述一个对象参数的属性

/**
 * @param {Object} employee - The employee who is responsible for the project.
 * @param {string} employee.name - The name of the employee.
 * @param {string} employee.department - The employee"s department.
 */

同样,可以联想到如果假如 employee 参数是一个数组,这个数组中包含 namedepartment 元素,那么可以这么描述。

描述参数的属性值在数组中

/**
 * Assign the project to a list of employees.
 * @param {Object[]} employees - The employees who are responsible for the project.
 * @param {string} employees[].name - The name of an employee.
 * @param {string} employees[].department - The employee"s department.
 */

可选参数和默认值

JSDoc可选参数

/**
 * @param {string} key - Key to be escaped.
 */

JSDoc可选参数和默认值

/**
 * @param {Number} [index=0] - Somebody"s name.
 */

Google Closure Compiler 可选参数

/**
 * @param {string=} somebody - Somebody"s name.
 */

多种类型参数

下面的例子演示了如何使用类型的表达式来表示一个参数可以接受多种类型(或任何类型),还有一个参数可以被多次使用。

/**
 * @param {(string|string[])} [somebody=John Doe] - Somebody"s name, or an array of names.
 */

允许任何类型

/**
 * @param {*} component - A component that could contain a manual key.
 */

可重复使用的参数

所有可变参数都是数字

/**
 * @param {...number} num - A positive or negative number.
 */

管道字符 | 用来连接联合类型,联合类型用来表明参数可以有多个类型

/** 
 * @param {string|null|undefined} str 
 */

因为参数是否非空很常见,因此有一个快捷方式用来标明联合类型是否包含 null

/** 
 * @param {?string} str1 is a string or null 
 * @param {?string|undefined} str2 is a string, null, or undefined. The ? 
 *     prefix does not include undefined, so it must be included explicitly. 
 */

除了基本类型的所有类型,如Object,Array和HTMLDocument默认都可以为空,这些类型统称为对象类型,因此 ? 前缀对对象类型是多余的

/** 
 * @param {Document} doc1 is a Document or null because object types are nullable by default. 
 * @param {?Document} doc2 is also a Document or null. 
 */

如果要声明一个非空对象对开,可以用 ! 前缀

/** 
 * @param {!Array} array must be a non-null Array 
 * @param {!Array|undefined} maybeArray is an Array or undefined, but 
 *     cannot be null. 
 */

尽管一个方法中可以有任意多个可选参数,但是可选参数不应该出现在必须参数之前,如果出现在之前,代码必须写成如下形式

/** 
 * @param {string=} title Defaults to "New Spreadsheet". 
 * @param {string} author 
 */

大量的可靠参数,最好将其它移到一个必须的对象参数中

/**  
 * @param {{author: (string|undefined), title: (string|undefined), numRows: (number|undefined)  
 */

不定数量参数

Closure不定数量参数

/** 
 * @param {string} category 
 * @param {...} purchases 
 * @return {number} 
 */

注释函数类型值的可选和可变参数

/** 
 * @param {function(string, string=)} ……
 * @param {function(string, ...[number]): number} ……
 */

回调函数

使用 @callback 标签来定义回调类型,回调类型包含到 @param 中。

/**
 * This callback type is called `requestCallback` and is displayed as a global symbol.
 *
 * @callback requestCallback
 * @param {number} responseCode
 * @param {string} responseMessage
 */

/**
 * Does something asynchronously and executes the callback on completion.
 * @param {requestCallback} cb - The callback that handles the response.
 */

@return / @returns 返回值的类型和描述

/**
 * @return {object} Object containing the ordered map of results.
 * @return {!number} The number of children in this subtree.
 */

或者

/**
 * @returns {boolean} True if mounted, false otherwise.
 * @returns {Element|Object|Boolean} The input node if acceptable, otherwise a falsy value
 */

更多示例

/**
 * @param {?Function(*, Boolean)} callback - To invoke upon traversing each child.
 * @param {String|Number} ref
 * @param {?*} children - Children tree container.
 * @param {Element|Object=} context
 * @param {!ArrayLike} nodes
 * @returns {Element|Object|Boolean} The input node if acceptable, otherwise a falsy value
 */

/**
 * @param {?*} children - Children tree container.
 * @param {!string} nameSoFar - Name of the key path so far.
 * @param {!function} callback - Callback to invoke with each child found.
 * @return {!number} The number of children in this subtree.
 */
更多请参考

以下网站为本文参考,欢迎留言纠正。

YUIDoc Syntax Reference
@use JSDoc
Closure javascript注解
Airbnb JavaScript Style Guide() { - 注释
原文Airbnb JavaScript Style Guide() {
js/javascript代码注释规范与示例
Js注释

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/95340.html

相关文章

  • Web前端开发规范手册

    摘要:规范目的为提高团队协作效率便于后台人员添加功能及前端后期优化维护输出高质量的文档特制订此文档。 规范目的 为提高团队协作效率, 便于后台人员添加功能及前端后期优化维护, 输出高质量的文档, 特制订此文档。 文件规范 文件命名规则 文件名称统一用小写的英文字母、数字和下划线的组合,其中不得包含汉字、空格和特殊字符;命名原则的指导思想一是使得你自己和工作组的每一个成员能够方便的理解每一个...

    wua_wua2012 评论0 收藏0
  • Web前端开发规范手册

    摘要:规范目的为提高团队协作效率便于后台人员添加功能及前端后期优化维护输出高质量的文档特制订此文档。 规范目的 为提高团队协作效率, 便于后台人员添加功能及前端后期优化维护, 输出高质量的文档, 特制订此文档。 文件规范 文件命名规则 文件名称统一用小写的英文字母、数字和下划线的组合,其中不得包含汉字、空格和特殊字符;命名原则的指导思想一是使得你自己和工作组的每一个成员能够方便的理解每一个...

    Tamic 评论0 收藏0
  • Web前端开发规范手册

    摘要:规范目的为提高团队协作效率便于后台人员添加功能及前端后期优化维护输出高质量的文档特制订此文档。 规范目的 为提高团队协作效率, 便于后台人员添加功能及前端后期优化维护, 输出高质量的文档, 特制订此文档。 文件规范 文件命名规则 文件名称统一用小写的英文字母、数字和下划线的组合,其中不得包含汉字、空格和特殊字符;命名原则的指导思想一是使得你自己和工作组的每一个成员能够方便的理解每一个...

    xiaotianyi 评论0 收藏0
  • nej jst模板语言标签

    摘要:为内置变量,值为列表长度,上例中值为。语法备注循环时包含和值范例备注为内置变量,值为循环的索引值。描述遍历表语法注子语句为可选范例注为内置变量,值为当前项的键值。 复制到这里一下,方便日后查询,源地址如果模板中存在 将/换成/ 如何使用jst模板 代码举例: 序号姓名性别 {if !defined(worke...

    马永翠 评论0 收藏0
  • 重构-改善既有代码的设计(五)--重构列表

    摘要:什么是重构列表重构方法需要以一种特定的格式记录下来。这些重构手法到底有多成熟本书中提到的重构手法第章。做法创造新函数,以用途命名提炼代码到函数中检查变量名是否符合规范在源函数中,将被提炼代码替换为函数引用测试范例重构前重构后 什么是重构列表 重构方法需要以一种特定的格式记录下来。按照格式记录下来的重构方法的集合叫重构列表 重构的记录格式 每个重构手法可分为5个部分: 名称 构建重构词汇...

    davidac 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<