JavaScript规范-条件判断,函数块,注释

来源:互联网 发布:大数据怎么存储 编辑:程序博客网 时间:2024/06/02 00:11
条件表达式和等号


(1)合理使用 === 和 !== 以及 == 和 !=.
(2)合理使用表达式逻辑操作运算.
(3)条件表达式的强制类型转换遵循以下规则:
(4)对象 被计算为 true
(5)Undefined 被计算为 false
(6)Null 被计算为 false
(7)布尔值 被计算为 布尔的值
(8)数字 如果是 +0, -0, or NaN 被计算为 false , 否则为 true
(9)字符串 如果是空字符串 '' 则被计算为 false, 否则为 true


条件判断快捷方式


// good
if (name) {
  // ...stuff...
}


// good
if (collection.length) {
  // ...stuff...
}


给所有多行的块使用大括号


// good
if (test) return false;


// good
if (test) {
  return false;
}
// good
function() {
  return false;
}
注释


使用 /** ... */ 进行多行注释,包括描述,指定类型以及参数值和返回值
使用 // 进行单行注释,在评论对象的上面进行单行注释,注释前放一个空行.
// good
/**
 * make() returns a new element
 * based on the passed in tag name
 *
 * @param <String> tag
 * @return <Element> element
 */
function make(tag) {


  // ...stuff...


  return element;
}
// good
function getType() {
  console.log('fetching type...');


  // set the default type to 'no type'
  var type = this._type || 'no type';


  return type;

}


来源:http://www.codeceo.com/article/javascript-guide.html

0 0