JavaScript Tutorial 3: Control Structure

Javascript tutorial 系列基本上翻译自 “A re-introduction to JavaScript (JS Tutorial)” 加上自己学习过程中的心得体会.  这一篇是第三篇(接Javascript Tutorial (学习笔记) 2):

Control structures

JavaScript has a similar set of control structures to other languages in the C family. Conditional statements are supported by if and else; you can chain them together if you like:

var name = "kittens";
if (name == "puppies") {
  name += "!";
} else if (name == "kittens") {
  name += "!!";
} else {
  name = "!" + name;
}
name == "kittens!!"

JavaScript has while loops and do-while loops. The first is good for basic looping; the second for loops where you wish to ensure that the body of the loop is executed at least once:

while (true) {
  // an infinite loop!
}

var input;
do {
  input = get_input();
} while (inputIsNotValid(input))

JavaScript’s for loop is the same as that in C and Java (所以Javascript其实很方便学, 如果对C或者Java比较熟悉): it lets you provide the control information for your loop on a single line.

for (var i = 0; i < 5; i++) {
  // Will execute 5 times
}

The && and || operators use short-circuit logic, which means whether they will execute their second operand is dependent on the first. This is useful for checking for null objects before accessing their attributes:

var name = o && o.getName();

Or for setting default values (这里当otherName没有赋值的时候, 那么就是false的意思, 于是||操作符看后面的”default”, 那么整个表达式的值就是”default”):

var name = otherName || "default";

JavaScript has a ternary operator for conditional expressions:

var allowed = (age > 18) ? "yes" : "no";

The switch statement can be used for multiple branches based on a number or string:

switch(action) {
    case 'draw':
        drawit();
        break;
    case 'eat':
        eatit();
        break;
    default:
        donothing();
}

If you don’t add a break statement, execution will “fall through” to the next level 这里插一句,我们可以注意到上面那个例子的字符串用了单引号, 其实我觉得这里用的不是很好, 容易混淆初学者什么时候用单引号,什么时候用双引号, 其实Javascript里面貌似单引号和双引号的却别没有那么明显, 只要按照个人喜好选择一个然后code里面统一用就好了, 不要这边用双引号, 等会又开始用单引号, 这样才是最不好的, 当然如果要表示字符串字面值比如输出栓引号, 下面的四条语句都会输出正确的单引号, 双引号, 如果想和C或者java的用法统一, 就干脆不要去记什么区别以及字符串字面值, 统一用双引号以及转译字符就好了(符合C的习惯): 

//测试
 console.log('"Double Quotes"'); //output "Double Quotes"
 console.log("\"Double Quotes\""); //output "Double Quotes"
 console.log('\'Single Quotes\''); //output 'Single Quotes'
 console.log("'Single Quotes'"); //output 'Single Quotes'

This is very rarely what you want — in fact it’s worth specifically labelling deliberate fallthrough with a comment if you really meant it to aid debugging:

switch(a) {
    case 1: // fallthrough
    case 2:
        eatit();
        break;
    default:
        donothing();
}

The default clause is optional. You can have expressions in both the switch part and the cases if you like; comparisons take place between the two using the ===operator(注: switch语句里面的case匹配是用三个等号的比较, 即需要类型相同, value相同):

switch(1 + 3) {
    case 2 + 2:
        yay();
        break;
    default:
        neverhappens();
}
Written on August 27, 2013