使用 /* … / 来进行多行注释,特别是方法,每一个参数的含义及默认值 ```javascript // bad // make() returns a new element // based on the passed in tag name // // @param {String} tag // @return {Element} element function make(tag) {
// …
return element; }
// good /**
- @description: 方法描述
- @param {*} tag 参数描述 【默认值】
@return {} / function make(tag) {
// …
return element; } ```
- 使用 // 进行单行注释。 将单行注释放在需要注释的行的上方新行。 在注释之前放一个空行,除非它在块的第一行 ```javascript // bad const active = true; // is current tab
// good // is current tab const active = true;
// bad function getType() { console.log(‘fetching type…’); // set the default type to ‘no type’ const type = this.type || ‘no type’;
return type; }
// good function getType() { console.log(‘fetching type…’);
// set the default type to ‘no type’ const type = this.type || ‘no type’;
return type; }
// also good function getType() { // set the default type to ‘no type’ const type = this.type || ‘no type’;
return type; }
3. 用一个空格开始所有的注释,使它更容易阅读
```javascript
// bad
//is current tab
const active = true;
// good
// is current tab
const active = true;
// bad
/**
*@description: 方法描述
*@param {*} tag 参数描述 【默认值】
*@return {*}
*/
function make(tag) {
// ...
return element;
}
// good
/**
* @description: 方法描述
* @param {*} tag 参数描述 【默认值】
* @return {*}
*/
function make(tag) {
// ...
return element;
}
使用 FIXME 或者 TODO 开始你的注释可以帮助其他开发人员快速了解,如果你提出了一个需要重新审视的问题,或者你对需要实现的问题提出的解决方案。 这些不同于其他评论,因为他们是可操作的。 这些行为是 FIXME: — 需要解决这个问题 或者 TODO: — 需要被实现 ```javascript class Calculator extends Abacus { constructor() { super();
// FIXME: 这里不应该使用全局变量 total = 0; } }
class Calculator extends Abacus { constructor() { super();
// TODO: total 应该由一个 param 的选项配置
this.total = 0;
} } ```