代码注释规范

186 阅读2分钟

规范示例:

/**
 * @file 墙体功能类的全部代码
 * @copyright Rayshon 2022
 */
/**
 * cesium墙体功能类
 * @author Wang LiZhi <wanglizhigs@163.com>
 * @version 1.0.0
 * @class Wall
 * @classdesc 继承Cesium基础功能类实现Wall功能类
 * @extends { Cesium }
 */
class Wall extend Cesium {
  /**
   * 通过id创建墙体
   * @author Wang LiZhi
   * @param {number} id
   * @return {boolean} 是否创建成功
   * @memberof Wall
   */
  create(id: number): boolean {
    const isCreate = true;
    return isCreate;
  }
}

1 行内注释

说明:行内注释以两个斜线开始,以行尾结束。

语法:code // 这是行内注释

使用方式://(双斜线)与代码之间保留一个空格,并且//(双斜线)与注释文字之间保留一个空格。

命名建议:

// 用来显示一个解释的评论
// -> 用来显示表达式的结果,
// >用来显示 console 的输出结果,
复制代码

eg:

function test() { // 测试函数
  console.log('Hello World!'); // >Hello World!
  return 3 + 2// ->5
}复制代码

2 单行注释

说明:单行注释以两个斜线开始,以行尾结束。

语法:// 这是单行注释

使用方式:单独一行://(双斜线)与注释文字之间保留一个空格。

eg:

// 调用了一个函数;1)单独在一行
setTitle();复制代码

3 多行注释

说明:以 /* 开头, */ 结尾

语法:/* 注释说明 */

使用方法:若开始/和结束/都在一行,推荐采用单行注释。若至少三行注释时,第一行为/,最后行为/,其他行以开始,并且注释文字与保留一个空格。

eg:

/*
* 代码执行到这里后会调用setTitle()函数
* setTitle():设置title的值
*/
setTitle();
复制代码

4 函数(方法)注释

说明:函数(方法)注释也是多行注释的一种,但是包含了特殊的注释要求

语法:

/** 
* 函数说明 
* @关键字 
*/
复制代码

常用注释关键字

@class

语法:@class [ ]

说明:标识函数是一个构造器函数或者类

示例:

/**
 * 动物父类
 * @class Animal
 */
class Animal {
}

@access

语法:@access <private|protected|public>

说明:指定该成员的访问级别(私有 private,公共 public,或保护 protected)

@access private 等价于 @private;

@access protected 等价于 @protected;

@access public 等价于 @public;

示例:

/**
 * 动物父类
 * @class Animal
 */
class Animal {
  /** @public  */
  public name = '';
  /** @protected  */
  protected hobby = '';
  /** @private  */
  private id = 123;
}

@extends

语法:@extends 

说明:

示例:

/**
 * 动物父类
 * @class Animal
 */
class Animal {
    run() {}
    eat() {}
}

/**
 * @class Dog
 * @extends Animal
 */
class Dog extends Animal {
}

@author

语法:@author  []

说明:标识项目或函数的作者

示例:

/**
 * 狗实体类
 * @author Wang LiZhi <wanglizhigs@163.com>
 * @class Dog
 * @classdesc 继承动物类实现狗类
 * @extends {Animal}
 */
class Dog extends Animal {
  /**
   * 奔跑的函数
   * @author Wang LiZhi
   * @memberof Dog
   */
  run() {
    console.log(`${this.name}在奔跑!`);
  }

  /**
   * 吃的函数
   * @author Wang LiZhi
   * @param {stringfood
   * @return {*}  {boolean}
   * @memberof Dog
   */
  eat(foodstring): boolean {
    const isHungry = Math.random() > 0.5;
    console.log(`我吃了${food},${isHungry ? '我还没吃饱!' : '我吃饱了!'}`);
    return isHungry;
  }
}

@constant

语法:@constant [ ]

说明:标签指明这个对象是一个常量。

示例:

/** 
 * @constant
 * @type {number}
 * @default
 */
const legs 4;

@file

语法:@file <some file description text>

说明:标签提供文件的说明。

@copyright

语法:@copyright <some copyright text>

说明:标签是用来描述一个文件的版权信息。一般和@file 标签结合使用。

示例:

/**
 * @file 这个基础功能实现文件
 * @copyright Rayshon 2022
 */

@default

语法:@default [<some value>]

说明:标识变量的赋值

示例:

/** 
 * @constant
 * @type {number}
 * @default
 */
const legs 4;

@deprecated

语法:@deprecated [<some text>]

说明:标签指明一个标识在你代码中已经被弃用。

示例:

/**
 * @deprecated since version 2.0
 */
function old({
}

@enum

语法:@enum []

说明:描述枚举成员

示例:

/**
 * 性别枚举
 * @readonly 
 * @enum {number}
 */
enum GENDER {
  MAN,
  WOMAN,
}

@exports

语法:@exports 

说明:标签描述由JavaScript模块的exports或module.exports属性导出的任何内容。

示例:


/**
 * 狗实体类
 * @author Wang LiZhi <wanglizhigs@163.com>
 * @export
 * @class Dog
 * @classdesc 继承动物类实现狗类
 * @extends {Animal}
 */
export default class Dog extends Animal {}

@param

语法:@param 

说明:标签提供了对某个函数的参数的各项说明,包括参数名、参数数据类型、描述等。

示例:


  /**
   * 吃的函数
   * @author Wang LiZhi
   * @param {stringfood
   * @return {*}  {boolean}
   * @memberof Dog
   */
  eat(foodstring): boolean {
    const isHungry = Math.random() > 0.5;
    console.log(`我吃了${food},${isHungry ? '我还没吃饱!' : '我吃饱了!'}`);
    return isHungry;
  }

@property

语法:@property 

说明:标签描述类,命名空间或其它对象的静态属性列表。

示例:


/**
 * @property { string } 姓
 * @property { string } 名
 * @property { number } 性别
 * @property { boolean } 是否是宠物
 * @property { string[] } 爱好
 */
const Dog = {
  surname: '张',
  name: '三',
  gender: 1,
  isPet: true,
  hobby: ['唱歌','跳舞','睡觉']
}

@ renturns

语法:@renturns 

说明:标签描述一个函数的返回值。语法和@param类似。

示例:

/**
   * 吃的函数
   * @author Wang LiZhi
   * @param {stringfood
   * @return {*}  {boolean}
   * @memberof Dog
   */
  eat(foodstring): boolean {
    const isHungry = Math.random() > 0.5;
    console.log(`我吃了${food},${isHungry ? '我还没吃饱!' : '我吃饱了!'}`);
    return isHungry;
  }

@static

语法:@static

说明:标签标明一个在父类中的标识符不需实例即可使用。

示例:

/**
 * 获取类型
 * @static
 * @return { class } {Dog}
 * @memberof Dog
 */
static getType(): Dog {
  return Dog;
}

@version

语法:@static 

说明:标签后面的文本将被用于表示该项的版本。

示例:

/**
 * 动物抽象类
 * @version 1.0.0
 * @abstract
 * @class Animal
*/
abstract class Animal {}

/**
 * @file 这个基础功能实现文件
 * @copyright Rayshon 2022
 */


/**
 * 动物抽象类
 * @version 1.0.0
 * @abstract
 * @class Animal
*/
abstract class Animal {
  /**
   * 姓
   * @protected
   * @memberof Animal
   */
  protected surname;

  /**
   * 名称
   * @public
   * @memberof Animal
   */
  public name;

  /**
   * 编号
   * @private
   * @memberof Animal
   */
  private number;

  /**
   * 动物奔跑的函数
   * @abstract
   * @memberof Animal
   */
  abstract run(): void;

  /**
   * 动物吃的函数
   * @abstract
   * @param {stringfood
   * @return {*}  {boolean}
   * @memberof Animal
   */
  abstract eat(foodstring): boolean;

  interface son
}

/**
 * 宠物类
 * @interface Pet
 */
interface Pet {
  /**
   * 卖萌
   * @memberof Pet
   */
  cute(): void;
}


/**
 * 狗实体类
 * @author Wang LiZhi <wanglizhigs@163.com>
 * @export
 * @class Dog
 * @classdesc 继承动物类实现狗类
 * @extends {Animal}
 */
export default class Dog extends Animal implements Pet  {
  public legs;
  constructor(surname: string; name: string) {
    this.surname = surname;
    this.name = name;

    /** 
     * @constant
     * @type {number}
     * @default
     */
    const legs = 4;
    this.legs = legs;
  }

/**
 * 获取类型
 * @static
 * @return { class } {Dog}
 * @memberof Dog
 */
static getType(): Dog {
  return Dog;
}

  /**
   * 奔跑的函数
   * @author Wang LiZhi
   * @memberof Dog
   */
  run() {
    console.log(`${this.name}在奔跑!`);
  }

  /**
   * 吃的函数
   * @author Wang LiZhi
   * @param {stringfood
   * @return {*}  {boolean}
   * @memberof Dog
   */
  eat(foodstring): boolean {
    const isHungry = Math.random() > 0.5;
    console.log(`我吃了${food},${isHungry ? '我还没吃饱!' : '我吃饱了!'}`);
    return isHungry;
  }

  /**
   * @implements { Pet }
   */
  cute(): void {
    console.log(`${this.name}给主人卖萌了!`);
  }
}

new Dog('大黄');


/**
 * 性别枚举
 * @readonly 
 * @enum {number}
 */
enum GENDER {
  MAN,
  WOMAN,
}




/**
 * @property { string } 姓
 * @property { string } 名
 * @property { number } 性别
 * @property { boolean } 是否是宠物
 * @property { string[] } 爱好
 */
const Dog = {
  surname'张',
  name'三',
  gender1,
  isPettrue,
  hobby: ['唱歌','跳舞','睡觉']
}