CoffeeScript 數學——abs() 方法



說明

此方法接收一個整數並返回給定整數的絕對值。

語法

以下是此方法的語法。

Math.abs( x )

示例

以下示例演示了 CoffeeScript 中 abs() 方法的用法。將此程式碼儲存在一個名為 math_abs.coffee 的檔案中。

value = Math.abs(-1);
console.log "The absolute value of -1 is : " + value 
         
value = Math.abs(null);
console.log "The absolute value of null is : " + value 
         
value = Math.abs(20);
console.log "The absolute value of 20 is : " + value

開啟 命令提示符 並像下面這樣編譯.coffee 檔案。

c:\> coffee -c math_abs.coffee

編譯後,它給你以下 JavaScript。

// Generated by CoffeeScript 1.10.0
(function() {
  var value;

  value = Math.abs(-1);

  console.log("The absolute value of -1 is : " + value);

  value = Math.abs(null);

  console.log("The absolute value of null is : " + value);

  value = Math.abs(20);

  console.log("The absolute value of 20 is : " + value);

}).call(this); 

現在,再次開啟 命令提示符,並像下面這樣執行 CoffeeScript 檔案。

c:\> coffee math_abs.coffee

執行後,CoffeeScript 檔案會列印以下輸出。

The absolute value of -1 is : 1
The absolute value of null is : 0
The absolute value of 20 is : 20
coffeescript_math.htm
廣告
© . All rights reserved.