- CoffeeScript 教程
- CoffeeScript——總覽
- CoffeeScript——概覽
- CoffeeScript——環境
- CoffeeScript——命令列實用工具
- CoffeeScript——語法
- CoffeeScript——資料型別
- CoffeeScript——變數
- CoffeeScript——運算子和別名
- CoffeeScript——條件判斷
- CoffeeScript——迴圈
- CoffeeScript——推導
- CoffeeScript——函式
- CoffeeScript 面向物件
- CoffeeScript——字串
- CoffeeScript——陣列
- CoffeeScript——物件
- CoffeeScript——範圍
- CoffeeScript——Splat
- CoffeeScript——日期
- CoffeeScript——數學
- CoffeeScript——異常處理
- CoffeeScript——正則表示式
- CoffeeScript——類和繼承
- CoffeeScript 高階
- CoffeeScript——Ajax
- CoffeeScript——jQuery
- CoffeeScript——MongoDB
- CoffeeScript——SQLite
- CoffeeScript 有用資源
- CoffeeScript——快速指南
- CoffeeScript——有用資源
- CoffeeScript——討論
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
廣告