比較運算子別名



以下表格顯示了部分比較運算子的別名。假設 A 持有 20,而變數 B 持有 20

運算子 別名 示例
= =(等號) is A is B 給出 true。
!= =(不等於號) isnt A isnt B 給出 false。

示例

以下程式碼展示如何在 CoffeeScript 中使用比較運算子的別名。將此程式碼儲存在名為 comparison_aliases.coffee 的檔案中

a = 10
b = 20
console.log "The result of (a is b) is "
result = a is b
console.log result

console.log "The result of (a isnt b) is "
result = a isnt b
console.log result

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

c:/> coffee -c comparison_aliases.coffee

編譯後,它會給出一個以下 JavaScript。

// Generated by CoffeeScript 1.10.0
(function() {
  var a, b, result;

  a = 10;

  b = 20;

  console.log("The result of (a is b) is ");

  result = a === b;

  console.log(result);

  console.log("The result of (a isnt b) is ");

  result = a !== b;

  console.log(result);

}).call(this);

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

c:/> coffee comparison_aliases.coffee

執行後,此 CoffeeScript 檔案就會生成以下輸出。

The result of (a is b) is
false
The result of (a isnt b) is
true
coffeescript_operators_and_aliases.htm
廣告