在Coffeescription中调用方法并传递正确的上下文
Calling methods and passing proper context in Coffeescript
这是我拥有的class
的简化版本:
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
方法的当前实现不起作用,因为我不知道如何在那里传递所需的上下文。这可能吗?
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()
相关文章:
- 将函数的上下文应用于javascript变量
- Twitter Bootstrap typeahead:使用“this”获取上下文/调用元素
- 使用JQuery的动态上下文菜单
- 如何访问UIWebView'的子窗口上下文
- JQuery在单击正文时隐藏上下文菜单
- JQuery上下文菜单显示/隐藏问题
- 如何从HTTP上下文对象中获取Post数据
- HTML字符串作为上下文
- 为什么我的上下文选择器和.buttonset()在ie中花费了这么长时间
- 丢失对象“;这个“;方法中的上下文
- 防止在移动Safari(iPad/iPhone)中长按/长按默认上下文菜单
- d3防止在上下文菜单上触发mouseout
- 是否可以在不更改上下文的情况下调用函数.apply
- 调用$.each()函数时上下文发生变化
- 何时可以;我的用户脚本在Javascript中触发右键单击(上下文菜单)
- 如何向onClick事件处理程序传递一个接受参数的函数,并且仍然将该函数绑定到组件's”;这个“;上下文
- Ember.js:将Em.$.getJSON转换为promise并将响应绑定到控制器上下文的正确方法
- File Upload事件上下文和javascript
- 使用JSTree上下文菜单捕获新创建的节点
- 访问函数对象的上下文属性|如何