
- CoffeeScript 教程
- CoffeeScript - 首頁
- CoffeeScript - 概覽
- CoffeeScript - 環境
- CoffeeScript - 命令列工具
- CoffeeScript - 語法
- CoffeeScript - 資料型別
- CoffeeScript - 變數
- CoffeeScript - 運算子和別名
- CoffeeScript - 條件語句
- CoffeeScript - 迴圈
- CoffeeScript - 推導式
- CoffeeScript - 函式
- CoffeeScript 面向物件
- CoffeeScript - 字串
- CoffeeScript - 陣列
- CoffeeScript - 物件
- CoffeeScript - 範圍
- CoffeeScript - 展開運算子
- CoffeeScript - 日期
- CoffeeScript - 數學
- CoffeeScript - 異常處理
- CoffeeScript - 正則表示式
- CoffeeScript - 類和繼承
- CoffeeScript 高階
- CoffeeScript - Ajax
- CoffeeScript - jQuery
- CoffeeScript - MongoDB
- CoffeeScript - SQLite
- CoffeeScript 有用資源
- CoffeeScript - 快速指南
- CoffeeScript - 有用資源
- CoffeeScript - 討論
CoffeeScript - 算術運算子
CoffeeScript 支援以下算術運算子。假設變數A 為10,變數B 為20,則−
序號 | 運算子和描述 | 示例 |
---|---|---|
1 | + (加法) 將兩個運算元相加 |
A + B = 30 |
2 | − (減法) 從第一個運算元中減去第二個運算元 |
A - B = -10 |
3 | * (乘法) 將兩個運算元相乘 |
A * B = 200 |
4 | / (除法) 將分子除以分母 |
B / A = 2 |
5 | % (取模) 輸出整數除法的餘數 |
B % A = 0 |
6 | ++ (自增) 將整數的值增加 1 |
A++ = 11 |
7 | -- (自減) 將整數的值減少 1 |
A-- = 9 |
示例
以下示例演示如何在 CoffeeScript 中使用算術運算子。將此程式碼儲存在名為arithmetic_example.coffee的檔案中
a = 33 b = 10 c = "test" console.log "The value of a + b = is" result = a + b console.log result result = a - b console.log "The value of a - b = is " console.log result console.log "The value of a / b = is" result = a / b console.log result console.log "The value of a % b = is" result = a % b console.log result console.log "The value of a + b + c = is" result = a + b + c console.log result a = ++a console.log "The value of ++a = is" result = ++a console.log result b = --b console.log "The value of --b = is" result = --b console.log result
開啟命令提示符並編譯 .coffee 檔案,如下所示。
c:\> coffee -c arithmetic_example.coffee
編譯後,它會為您提供以下 JavaScript 程式碼。
// Generated by CoffeeScript 1.10.0 (function() { var a, b, c, result; a = 33; b = 10; c = "test"; console.log("The value of a + b = is"); result = a + b; console.log(result); result = a - b; console.log("The value of a - b = is "); console.log(result); console.log("The value of a / b = is"); result = a / b; console.log(result); console.log("The value of a % b = is"); result = a % b; console.log(result); console.log("The value of a + b + c = is"); result = a + b + c; console.log(result); a = ++a; console.log("The value of ++a = is"); result = ++a; console.log(result); b = --b; console.log("The value of --b = is"); result = --b; console.log(result); }).call(this);
現在,再次開啟命令提示符並執行 CoffeeScript 檔案,如下所示。
c:\> coffee arithmetic_example.coffee
執行後,CoffeeScript 檔案會產生以下輸出。
The value of a + b = is 43 The value of a - b = is 23 The value of a / b = is 3.3 The value of a % b = is 3 The value of a + b + c = is 43test The value of ++a = is 35 The value of --b = is 8
coffeescript_operators_and_aliases.htm
廣告