函数组合、状态与可变性:CoffeeScript 编程的关键要点
1. 函数组合
1.1 定义基础函数
在计算利润时,需要先定义一些基础函数和变量。例如,定义了 overhead (管理费用)、 costPrice (成本价格)和 numberSold (销售数量)函数:
overhead = 140
costPrice = 100
numberSold = (salePrice) ->
50 + 20/10 * (200 - salePrice)
在此基础上,进一步定义了 revenue (收入)、 cost (成本)和 profit (利润)函数:
revenue = (salePrice) ->
(numberSold salePrice) * salePrice
cost = (salePrice) ->
overhead + (numberSold salePrice) * costPrice
profit = (salePrice) ->
(revenue salePrice) – (cost salePrice)
为了提高代码的封装性,可以将 numberSold 、 <
超级会员免费看
订阅专栏 解锁全文
7756

被折叠的 条评论
为什么被折叠?



