JavaScript學習筆記-- The switch Statement

1. case 後沒有break的情況

switch (i) {
case 25:
/* falls through*/
case 35:
alert(“25 or 35”);
break;
case 45:
alert(“45”);
break;
default:
alert(“Other”);
}

2. switch 接收的參數可以不只是數字

switch (“hello world”) {
case “hello” + “world”:
alert(“Greeting was found.”);
break;
case “goodbye”:
alert(“Closing was found.”);
break;
default:
alert(“Unexpected message was found.”);
}

3. case的判斷內容可以是表達式

var num = 25;
switch (true) {
case num < 0:
alert(“Less than 0.”);
break;
case num >= 0&& num <= 10:
alert(“Between 0 and 10.”);
break;
case num > 10&& num <= 20:
alert(“Between 10 and 20.”);
break;
default:
alert(“More than 20.”);
}


參考:JavaScript for Web Developers,Third Edition,Nicholas C. Zakas


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章