show example

优质
小牛编辑
120浏览
2023-12-01

后缀如果

您可以使用后缀形式重写if语句,其中,要执行的语句后跟if和布尔表达式。

语法 (Syntax)

以下是postfix-if语句的语法。

Statements to be executed <b>if</b> expression

例子 (Example)

下面给出了postfix if语句的示例。 将以下示例保存在名为postfix_if_example.coffee的文件中

name = "Ramu"
score = 60
console.log "Congratulations you have passed the examination" if score>40

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

c:\> coffee -c postfix_if_example.coffee

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

// Generated by CoffeeScript 1.10.0
(function() {
  var name, score;
  name = "Ramu";
  score = 60;
  if (score > 40) {
    console.log("Congratulations you have passed the examination");
  }
}).call(this);

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

c:\> coffee postfix_if_example.coffee

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

Congratulations you have passed the exam

Postfix除非

您可以使用后缀形式重写unless语句,其中,要执行的语句后跟除非与布尔表达式一起使用。

语法 (Syntax)

以下是postfix-if语句的语法。

Statements to be executed <b>unless</b> expression

例子 (Example)

下面给出了postfix除非声明的示例。 将以下示例保存在名为postfix_unless_example.coffee的文件中

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

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

c:\> coffee -c postfix_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  postfix_unless_example.coffee

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

Sorry try again