目录

unless statement

unless语句是CoffeeScript提供的附加功能。 它与if语句完全相反。 如果给定条件为false则将执行unless块中的代码。

语法 (Syntax)

下面给出了CoffeeScript中的unless语句的语法。 就像if语句一样,它包含一个unless关键字,一个布尔表达式,以及一个代码块,如果给定的表达式为false ,它将被执行。

unless expression
   Statement(s) to be executed if the expression is false

流程图 (Flow Diagram)

除非声明

例子 (Example)

以下示例演示了CoffeeScript中unless语句的用法。 将此代码保存在名为unless_example.coffee的文件中

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

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

c:\> coffee -c unless_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_example.coffee

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

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