分享 CoffeeScript 的命令模式

i5ting · 2013年02月23日 · 1509 次阅读

有意思啊

http://coffeescriptcookbook.com/chapters/design_patterns/command

# Using a private variable to simulate external scripts or modules
incrementers = (() ->
    privateVar = 0

    singleIncrementer = () ->
        privateVar += 1

    doubleIncrementer = () ->
        privateVar += 2

    commands = 
        single: singleIncrementer
        double: doubleIncrementer
        value: -> privateVar
)()

class RunsAll
    constructor: (@commands...) ->
    run: -> command() for command in @commands

runner = new RunsAll(incrementers.single, incrementers.double, incrementers.single, incrementers.double)
runner.run()
incrementers.value() # => 6

coffeescriptcookbook 这个地方还是有挺多好东西的

暂无回复。
需要 登录 后方可回复, 如果你还没有账号请 注册新账号