日期:2014-05-16  浏览次数:20435 次

把普通javascript 替换成 coffeescript的步骤

1. 重新声明方法 (this.name? or @name)
function name(para)?? =>
this.name = (para) ->

2. 去掉大括号? ? (当传入参数大于等于1个时)
? if(true) => if true
? [a,b,c].size() => [a,b,c].size?? # 绝对不可以这样做。??

例如:??? [a,b,c].size() => 3

?[a,b,c].size =>? function(){ ....? }

那么在使用 if [a,b,c].size == 3 的时候,就会返回false.


3. 去掉尾部分号? ( in vim: :%s/;$ )
4. 去掉 var? (var name =1? =>? name = 1)
5. 使用 "#{}"风格的字符串连接: ?
?? "say"+ hi + "to" + name ?
?? "say#{hi}to#{name}"

6. hash: ?
? a = { d:1, b: 2}? =>
? a:
??? d: 1
??? b: 2

?

7. 替换三元表达式:

? a = true ? "good" : "bad"

? a = if true then "good" else "bad"

?

?