观察者模式
问题
当一个事件发生时你不得不向一些对象发布公告。
解决方案
使用观察者模式(Observer Pattern)。class PostOfficeconstructor: () ->@subscribers = []notifyNewItemReleased: (item) ->subscriber.callback(item) for subscriber in @subscribers when subscriber.item is itemsubscribe: (to, on
找到上一个月(或下一个月)
问题
你需要计算相关日期范围例如“上一个月”,“下一个月”。
解决方案
添加或减去当月的数字,JavaScript的日期构造函数会修复数学知识。# these examples were written in GMT-6
# Note that these examples WILL work in January!
now = new Date
# => "Sun, 08 May 2011 05:50:52 GMT"lastMonthStart =
For 循环
问题
你想通过一个for循环来迭代数组、对象或范围。
解决方案# for(i = 1; i<= 10; i++)
x for x in [1..10]
# => [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]# To count by 2
# for(i=1; i<= 10; i=i+2)
x for x in [1..10] by 2
# => [ 1, 3, 5, 7, 9 ]# Perform a simple operation like