课程表

CoffeeScript 语法

CoffeeScript 类和对象

CoffeeScript 字符串

CoffeeScript 数组

CoffeeScript 日期和时间

CoffeeScript 数学

CoffeeScript 方法

CoffeeScript 元编程

CoffeeScript jQuery

CoffeeScript 正则表达式

CoffeeScript 网络

CoffeeScript 设计模式

CoffeeScript 数据库

CoffeeScript 测试

工具箱
速查手册

操作符和 aliase

当前位置:免费教程 » JS/JS库/框架 » CoffeeScript

由于操作符 == 常常带来不准确的约束, 不容易达到效果, 而且跟其他语言当中意思不一致, CoffeeScript 会把 == 编译为 ===, 把 != 变异为 !==. 此外, is 编译为 ===, 而 isnt 编译为 !==.

not 可以作为 ! 的 alias 使用.

逻辑操作方面, and 编译为 &&, 而 or 编译为 ||.

while, if/else, switch/when 的语句当中, then 可以被用来分隔判断条件跟表达式, 这样就不用强制写换行或者分号了.

就像 YAML, onyestrue 是一样的, 而 offno 是布尔值 false.

unless 可以认为是 if 相反的版本.

this.property 简短的写法可以用 @property.

可以用 in 判断数据在数组中是否出现, 而 of 可以探测 JavaScript 对象的属性是否存在.

为了简化数学表达式, ** 可以用来表示乘方, // 表示整除, %% 提供数学的模运算(译注: true mathematical modulo?).

完整的列表:

CoffeeScriptJavaScript
is===
isnt!==
not!
and&&
or||
true, yes, ontrue
false, no, offfalse
@, thisthis
ofin
inno JS equivalent
a ** bMath.pow(a, b)
a // bMath.floor(a / b)
a %% b(a % b + b) % b
CoffeeScript:
  1. launch() if ignition is on
  2.  
  3. volume = 10 if band isnt SpinalTap
  4.  
  5. letTheWildRumpusBegin() unless answer is no
  6.  
  7. if car.speed < limit then accelerate()
  8.  
  9. winner = yes if pick in [47, 92, 13]
  10.  
  11. print inspect "My name is #{@name}"
编译成JS:
  1. var volume, winner;
  2.  
  3. if (ignition === true) {
  4. launch();
  5. }
  6.  
  7. if (band !== SpinalTap) {
  8. volume = 10;
  9. }
  10.  
  11. if (answer !== false) {
  12. letTheWildRumpusBegin();
  13. }
  14.  
  15. if (car.speed < limit) {
  16. accelerate();
  17. }
  18.  
  19. if (pick === 47 || pick === 92 || pick === 13) {
  20. winner = true;
  21. }
  22.  
  23. print(inspect("My name is " + this.name));
转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号