第三章 语句和表达式

//不好的写法

if(condition)
    doSomething()

//不好的写法

if(condition) doSomething()

//不好的写法

if(condition) { doSomething(); } 
不好的原因:

(1) 很多JS指南禁止这种写法,比如Jquery核心风格指南,google的js风格指南
(2) 省略花括号在JSLint和JSHint中会报警告

//好的写法

if(condition){
   doSomething();
}

3.1 花括号的对齐方式

(1) 继承自Java的(推荐使用)

if(condition){
   doSomething();
} else {
   doSomethingElse();
}

(2)继承自#C(不推荐使用)

if(condition)
{
   doSomething();
} 
else 
{
   doSomethingElse();
}

3.2 块语句间隔

(1) 不推荐

if(condition){
   doSomething();
} 

(2) 推荐

if (condition) {
   doSomething();
} 

(3) 不推荐

if ( condition ) {
   doSomething();
} 

3.3 Switch 语句

3.3.1 Switch的连续执行

switch(condition){

    case "first":
    case "second":

          //代码
          break;

    default:
          //代码
}

3.3.1 default

是否需要default?

推荐在没有默认行为且写了注释的情况下省略default

switch(condition){

    case "first":
          //代码
          break;

    case "second":
          //代码
          break;

    // 没有 default
}

3.4 with 语句

严格模式中,with语句是被明确禁止的如果使用则语法报错。
强烈推荐避免使用with,因为你无法将你的代码运行于严格模式中。

你可能感兴趣的:(第三章 语句和表达式)