2015-12-29 26 views
0

这是class简化版本我有:调用方法和传递正确的上下文中的CoffeeScript

class Calculations 
    constructor: (amount = 1000) -> 
    @amount = amount 

    rowOne: => 
    columnOne: => @amount * 0.1 
    columnTwo: => @amount * 0.2 
    columnThree: => @amount * 0.3 
    total:  => @_total(context) 

    _total: (context) -> 
    context.columnOne() + context.columnTwo() + context.columnThree() 

我想打电话给这样的方法:

calc = new Calculations() 

calc.rowOne().columnOne() # And it should return 100 
calc.rowOne().columnTwo() # And it should return 200 
calc.rowOne().columnThree() # And it should return 300 
calc.rowOne().total()  # And it should return 600 

我怎样才能实现这个正常吗?目前的_total方法的实现当然不起作用,因为我不知道如何在那里传递需要的上下文。这可能吗?

回答

1
class Calculations 
    constructor: (amount = 1000) -> 
    @amount = amount 

    rowOne: => 
    columnOne: => @amount * 0.1 
    columnTwo: => @amount * 0.2 
    columnThree: => @amount * 0.3 
    total:  @_total 

    _total:() -> 
    this.columnOne() + this.columnTwo() + this.columnThree() 
相关问题