Javascript 使Coffescript代码干燥
我在coffescript中有两个方法,它们如下所示:Javascript 使Coffescript代码干燥,javascript,coffeescript,Javascript,Coffeescript,我在coffescript中有两个方法,它们如下所示: amount_calculation_when_unit_price_change = (parent) -> $(".unit_price input").bind "keyup change", (e) -> unit_price = @value.replace(/\,/g,'.') quantity = $(this).closest(parent).find(".quantity inp
amount_calculation_when_unit_price_change = (parent) ->
$(".unit_price input").bind "keyup change", (e) ->
unit_price = @value.replace(/\,/g,'.')
quantity = $(this).closest(parent).find(".quantity input").val().replace(/\,/g,'.')
discount = $(this).closest(parent).find(".discount input").val().replace(/\,/g,'.') if $('.discount input').length > 1
if discount == ""
discount = 0
discount_type = $(this).closest(parent).find(".discount_type select").val() if $('.discount_type select').length > 1
value = ((parseFloat(unit_price) * parseFloat(quantity)))
if discount_type == "Absolute"
value = value - parseFloat(discount).toFixed(2)
else if discount_type == "Relative"
discount_price = value * parseFloat(discount/100).toFixed(2)
value = value - discount_price
amount = $(this).closest(parent).find(".amount")
if value.toString().match(pricePattern)
amount.html('<br/>' + '$' + "#{(value.toFixed(2))}")
else
amount.html('<br/>' + '$' + "0.00")
calculate_total()
amount_calculation_when_quantity_change = (parent) ->
$(".quantity input").bind "keyup change", (e) ->
quantity = @value.replace(/\,/g,'.')
unit_price = $(this).closest(parent).find(".unit_price input").val().replace(/\,/g,'.')
discount = $(this).closest(parent).find(".discount input").val().replace(/\,/g,'.') if $('.discount input').length > 1
if discount == ""
discount = 0
discount_type = $(this).closest(parent).find(".discount_type select").val() if $('.discount select').length > 1
value = ((parseFloat(unit_price) * parseFloat(quantity)))
if discount_type == "Absolute"
value = value - parseFloat(discount).toFixed(2)
else if discount_type == "Relative"
discount_price = value * parseFloat(discount/100).toFixed(2)
value = value - discount_price
amount = $(this).closest(parent).find(".amount")
if value.toString().match(pricePattern)
amount.html('<br/>' + '$' + "#{(value.toFixed(2))}")
else
amount.html('<br/>' + '$' + "0.00")
calculate_total()
amount_calculation_when_unit_price_change = (parent) ->
for class in ["unit_price, quantity"]
values = {}
$(".#{class} input").bind "keyup change", (e) ->
values[class] = @value.replace(/\,/g,'.')
...
单位价格变化时的金额计算=(父项)->
$(“.unit_price input”)。绑定“键控更改”,(e)->
单价=@value.replace(/\,/g,“.”)
数量=$(this).最近的(父项).查找(“.quantity input”).val().替换(/\,/g,,。)
折扣=$(this).closest(parent).find(“.discount input”).val()。如果$('.discount input')。length>1,则替换(/\,/g'.')
如果折扣=“”
折扣=0
折扣类型=$(this).closest(parent).find(“.discount类型选择”).val(),如果$('.discount类型选择')。length>1
值=((parseFloat(单价)*parseFloat(数量)))
如果折扣类型==“绝对”
value=value-parseFloat(折扣).toFixed(2)
如果折扣类型==“相对”,则为else
折扣价格=价值*浮动(折扣/100)。固定(2)
价值=价值-折扣价格
金额=$(此)。最近的(父项)。查找(“.amount”)
if value.toString()匹配(pricePattern)
amount.html('
'+'$'+“{(value.toFixed(2))}”)
其他的
amount.html(“
”+“$”+“0.00”)
计算总数()
当数量变化=(父项)->
$(“.quantity input”).bind“keyup change”,(e)->
数量=@value.replace(/\,/g,“.”)
单价=$(this).closest(父项).find(“.unit\u price input”).val().replace(/\,/g,“.”)
折扣=$(this).closest(parent).find(“.discount input”).val()。如果$('.discount input')。length>1,则替换(/\,/g'.')
如果折扣=“”
折扣=0
折扣类型=$(this).closest(parent).find(“.discount类型选择”).val()(如果$('.discount类型选择”).length>1
值=((parseFloat(单价)*parseFloat(数量)))
如果折扣类型==“绝对”
value=value-parseFloat(折扣).toFixed(2)
如果折扣类型==“相对”,则为else
折扣价格=价值*浮动(折扣/100)。固定(2)
价值=价值-折扣价格
金额=$(此)。最近的(父项)。查找(“.amount”)
if value.toString()匹配(pricePattern)
amount.html('
'+'$'+“{(value.toFixed(2))}”)
其他的
amount.html(“
”+“$”+“0.00”)
计算总数()
就像每个人看到的一样,有很多重复,这段代码看起来非常难看。
如何使此代码更加干燥?尝试以下方法:
amount_calculation_when_unit_price_change = (parent) ->
$(".unit_price input").bind "keyup change", (e) ->
unit_price = @value.replace(/\,/g,'.')
quantity = $(this).closest(parent).find(".quantity input").val().replace(/\,/g,'.')
discount = $(this).closest(parent).find(".discount input").val().replace(/\,/g,'.') if $('.discount input').length > 1
if discount == ""
discount = 0
discount_type = $(this).closest(parent).find(".discount_type select").val() if $('.discount_type select').length > 1
value = ((parseFloat(unit_price) * parseFloat(quantity)))
if discount_type == "Absolute"
value = value - parseFloat(discount).toFixed(2)
else if discount_type == "Relative"
discount_price = value * parseFloat(discount/100).toFixed(2)
value = value - discount_price
amount = $(this).closest(parent).find(".amount")
if value.toString().match(pricePattern)
amount.html('<br/>' + '$' + "#{(value.toFixed(2))}")
else
amount.html('<br/>' + '$' + "0.00")
calculate_total()
amount_calculation_when_quantity_change = (parent) ->
$(".quantity input").bind "keyup change", (e) ->
quantity = @value.replace(/\,/g,'.')
unit_price = $(this).closest(parent).find(".unit_price input").val().replace(/\,/g,'.')
discount = $(this).closest(parent).find(".discount input").val().replace(/\,/g,'.') if $('.discount input').length > 1
if discount == ""
discount = 0
discount_type = $(this).closest(parent).find(".discount_type select").val() if $('.discount select').length > 1
value = ((parseFloat(unit_price) * parseFloat(quantity)))
if discount_type == "Absolute"
value = value - parseFloat(discount).toFixed(2)
else if discount_type == "Relative"
discount_price = value * parseFloat(discount/100).toFixed(2)
value = value - discount_price
amount = $(this).closest(parent).find(".amount")
if value.toString().match(pricePattern)
amount.html('<br/>' + '$' + "#{(value.toFixed(2))}")
else
amount.html('<br/>' + '$' + "0.00")
calculate_total()
amount_calculation_when_unit_price_change = (parent) ->
for class in ["unit_price, quantity"]
values = {}
$(".#{class} input").bind "keyup change", (e) ->
values[class] = @value.replace(/\,/g,'.')
...
绑定的两个事件处理程序看起来完全相同。那么,为什么不将其定义为一个函数,并将该函数作为每个事件的处理程序传递呢?您可以更详细地解释一下,或者给我举个简单的例子吗?