CoffeeScript - switch 語句



switch 語句允許測試一個變數是否等於一個值列表中的某個值。每個值稱為一個case,被切換的變數會針對每個 switch case 進行檢查。以下是 JavaScript 中switch 的語法。

switch (expression){
   case condition 1: statement(s)
   break;   
   
   case condition 2: statement(s)
   break;
      
   case condition n: statement(s)
   break;
   
   default: statement(s)
}

在 JavaScript 中,每個 switch case 之後,我們必須使用break 語句。如果我們不小心忘記了break 語句,則有可能從一個 switch case 掉到另一個 switch case。

CoffeeScript 中的 Switch 語句

CoffeeScript 透過使用switch-when-else子句的組合來解決這個問題。這裡有一個可選的 switch 表示式,後跟 case 語句。

每個 case 語句都有兩個子句whenthenwhen 後面跟著條件,then 後面跟著一組語句,如果滿足特定條件,則執行這些語句。最後,我們有一個可選的else 子句,其中包含預設條件的操作。

語法

以下是 CoffeeScript 中switch 語句的語法。我們指定沒有括號的表示式,並透過保持適當的縮排來分隔 case 語句。

switch expression
   when condition1 then statements
   when condition2 then statements
   when condition3 then statements
   else statements

流程圖

CoffeeScript switch statement

示例

以下示例演示了在 CoffeeScript 中使用switch 語句的方法。將此程式碼儲存在名為switch_example.coffee 的檔案中。

name="Ramu"
score=75
message = switch 
   when score>=75 then "Congrats your grade is A"
   when score>=60 then "Your grade is B"
   when score>=50 then "Your grade is C"
   when score>=35 then "Your grade is D"
   else "Your grade is F and you are failed in the exam"
console.log message

開啟命令提示符並編譯 .coffee 檔案,如下所示。

c:\> coffee -c switch_exmple.coffee

編譯後,它會生成以下 JavaScript 程式碼。

// Generated by CoffeeScript 1.10.0
(function() {
  var message, name, score;

  name = "Ramu";

  score = 75;

  message = (function() {
    switch (false) {
      case !(score >= 75):
        return "Congrats your grade is A";
      case !(score >= 60):
        return "Your grade is B";
      case !(score >= 50):
        return "Your grade is C";
      case !(score >= 35):
        return "Your grade is D";
      default:
        return "Your grade is F and you are failed in the exam";
    }
  })();

  console.log(message);

}).call(this);

現在,再次開啟命令提示符並執行 CoffeeScript 檔案,如下所示:

c:\> coffee switch_exmple.coffee

執行後,CoffeeScript 檔案會產生以下輸出。

Congrats your grade is A

when 子句的多個值

我們還可以透過在 switch case 中使用逗號 (,) 分隔它們來為單個 when 子句指定多個值。

示例

以下示例顯示瞭如何透過為when 子句指定多個值來編寫 CoffeeScript switch 語句。將此程式碼儲存在名為switch_multiple_example.coffee 的檔案中。

name="Ramu"
score=75
message = switch name
   when "Ramu","Mohammed" then "You have passed the examination with grade A"
   when "John","Julia" then "You have passed the examination with grade is B"
   when "Rajan" then "Sorry you failed in the examination"
   else "No result"
console.log message

開啟命令提示符並編譯 .coffee 檔案,如下所示。

c:\> coffee -c switch_multiple_example.coffee

編譯後,它會生成以下 JavaScript 程式碼。

// Generated by CoffeeScript 1.10.0
(function() {
  var message, name, score;

  name = "Ramu";

  score = 75;

  message = (function() {
    switch (name) {
      case "Ramu":
      case "Mohammed":
        return "You have passed the examination with grade A";
      case "John":
      case "Julia":
        return "You have passed the examination with grade is B";
      case "Rajan":
        return "Sorry you failed in the examination";
      default:
        return "No result";
    }
  })();

  console.log(message);

}).call(this);

現在,再次開啟命令提示符並執行 CoffeeScript 檔案,如下所示:

c:\> coffee switch_multiple_example.coffee

執行後,CoffeeScript 檔案會產生以下輸出。

You have passed the examination with grade A 
coffeescript_conditionals.htm
廣告