Coffeescript 咖啡脚本回忆录?

Coffeescript 咖啡脚本回忆录?,coffeescript,Coffeescript,我有一个函数,它将数字显示为格式正确的价格(美元) var showPrice=(函数(){ var commaRe=/([^,$])(\d{3})\b/; 返回函数(价格){ var格式化=(价格 commaRe=/([^,$])(\d{3})\b/ (价格)-> 格式化=(如果价格 commaRe=/([^,$])(\d{3})\b/ (价格)-> 格式化=(如果价格 commaRe=/([^,$])(\d{3})\b/ (价格)-> 格式化=((如果价格 commaRe=/([^,$])(

我有一个函数,它将数字显示为格式正确的价格(美元)

var showPrice=(函数(){
var commaRe=/([^,$])(\d{3})\b/;
返回函数(价格){
var格式化=(价格<0?-“:”)+“$”+数学.abs(数字(价格)).toFixed(2);
while(commaRe.test(格式化)){
格式化=格式化。替换(逗号,“$1,$2”);
}
返回格式;
}
})();

据我所知,反复使用的正则表达式应该存储在一个变量中,以便只编译一次。假设这仍然是真的,那么该如何在Coffeescript中重写此代码?

这在Coffeescript中是等效的

showPrice = do ->
  commaRe = /([^,$])(\d{3})\b/
  (price) ->
    formatted = (if price < 0 then "-" else "") + "$" + Math.abs(Number price).toFixed(2)
    while commaRe.test(formatted)
      formatted = formatted.replace commaRe, "$1,$2"
    formatted
showPrice=do->
commaRe=/([^,$])(\d{3})\b/
(价格)->
格式化=(如果价格<0,则“-”else”“)+“$”+数学.abs(数字价格).toFixed(2)
而commaRe.test(格式化)
格式化=格式化。替换commaRe,“$1,$2”
格式化

这在CoffeeScript中是等效的

showPrice = do ->
  commaRe = /([^,$])(\d{3})\b/
  (price) ->
    formatted = (if price < 0 then "-" else "") + "$" + Math.abs(Number price).toFixed(2)
    while commaRe.test(formatted)
      formatted = formatted.replace commaRe, "$1,$2"
    formatted
showPrice=do->
commaRe=/([^,$])(\d{3})\b/
(价格)->
格式化=(如果价格<0,则“-”else”“)+“$”+数学.abs(数字价格).toFixed(2)
而commaRe.test(格式化)
格式化=格式化。替换commaRe,“$1,$2”
格式化

您可以使用将JavaScript代码翻译成咖啡脚本。对于给定代码,结果为:

showPrice = (->
  commaRe = /([^,$])(\d{3})\b/
  (price) ->
    formatted = ((if price < 0 then "-" else "")) + "$" + Math.abs(Number(price)).toFixed(2)
    formatted = formatted.replace(commaRe, "$1,$2")  while commaRe.test(formatted)
    formatted
)()
showPrice=(->
commaRe=/([^,$])(\d{3})\b/
(价格)->
格式化=((如果价格<0,则为“-”else“)+“$”+Math.abs(数字(价格)).toFixed(2)
格式化=格式化的.replace(commaRe,“$1,$2”),而commaRe.test(格式化)
格式化
)()
我自己的说法是:

showPrice = do ->
  commaRe = /([^,$])(\d{3})\b/
  (price) ->
    formatted = (if price < 0 then '-' else '') + '$' +
                Math.abs(Number price).toFixed(2)
    while commaRe.test formatted
      formatted = formatted.replace commaRe, '$1,$2'
    formatted
showPrice=do->
commaRe=/([^,$])(\d{3})\b/
(价格)->
格式化=(如果价格<0,则为“-”其他“”)+“$”+
数学.abs(数字价格).toFixed(2)
而commaRe.test是格式化的
格式化=格式化。替换命令,$1,$2'
格式化

至于重复使用的正则表达式,我不知道。

您可以使用。对于给定代码,结果为:

showPrice = (->
  commaRe = /([^,$])(\d{3})\b/
  (price) ->
    formatted = ((if price < 0 then "-" else "")) + "$" + Math.abs(Number(price)).toFixed(2)
    formatted = formatted.replace(commaRe, "$1,$2")  while commaRe.test(formatted)
    formatted
)()
showPrice=(->
commaRe=/([^,$])(\d{3})\b/
(价格)->
格式化=((如果价格<0,则为“-”else“)+“$”+Math.abs(数字(价格)).toFixed(2)
格式化=格式化的.replace(commaRe,“$1,$2”),而commaRe.test(格式化)
格式化
)()
我自己的说法是:

showPrice = do ->
  commaRe = /([^,$])(\d{3})\b/
  (price) ->
    formatted = (if price < 0 then '-' else '') + '$' +
                Math.abs(Number price).toFixed(2)
    while commaRe.test formatted
      formatted = formatted.replace commaRe, '$1,$2'
    formatted
showPrice=do->
commaRe=/([^,$])(\d{3})\b/
(价格)->
格式化=(如果价格<0,则为“-”其他“”)+“$”+
数学.abs(数字价格).toFixed(2)
而commaRe.test是格式化的
格式化=格式化。替换命令,$1,$2'
格式化

至于重复使用的正则表达式,我不知道。

很酷,谢谢:-p我想
do->
正是我想要的…我能找到的唯一其他选择是
(()->…)()
,它看起来太像Javascript了:-p@thirdender不客气。作为提示,您可以对没有参数的函数执行
->…
。咖啡足够聪明来处理这个问题。不需要
()->…
酷,谢谢:-p我想
做->
正是我想要的…我能找到的唯一其他选择是
(()->…)()
,它看起来太像Javascript了:-p@thirdender不客气。作为提示,您可以对没有参数的函数执行
->…
。咖啡足够聪明来处理这个问题。不需要
()->…