目录

unless-then statement

使用unless-then语句,我们可以在一行中编写CoffeeScript的unless语句。 它由一个布尔表达式后跟then关键字组成,后跟一个或多个语句。 当给定的布尔表达式为false时,将执行这些语句。

语法 (Syntax)

以下是CoffeeScript中的unless-then语句的语法。

unless expression <b class="notranslate">then</b> Statement(s) to be executed if expression is false

例子 (Example)

下面给出了CoffeeScript的unless-then语句的示例。 将以下示例保存在名为unless_then_example.coffee的文件中

name = "Ramu"
score = 30
unless score>=40 then console.log "Sorry try again"

打开command prompt并编译.coffee文件,如下所示。

c:\> coffee -c unless_then_example.coffee

在编译时,它为您提供以下JavaScript。

// Generated by CoffeeScript 1.10.0
(function() {
  var name, score;
  name = "Ramu";
  score = 30;
  if (!(score >= 40)) {
    console.log("Sorry try again");
  }
}).call(this);

现在,再次打开command prompt并运行CoffeeScript文件 -

c:\> coffee unless_then_example.coffee

执行时,CoffeeScript文件生成以下输出。

Sorry try again
↑回到顶部↑
WIKI教程 @2018