【发布时间】:2015-10-18 02:32:11
【问题描述】:
我今天开始使用 CoffeeScript,发现自己在需要回调函数时经常使用像 (args...) => @style(args...) 这样的模式。上下文大致如下:
class Parent
@style: (feature) ->
if feature
@insight()
class Child extends Parent
@insight: ->
alert 'Sara is awesome'
@load: ->
[42].forEach((args...) => @style(args...))
Child.load()
这显示Sara is awesome,这是准确的。如果我只使用[42].forEach(@style),style 最终会得到一个 this 指代父类(我认为?),它不知道 insight。
但这很冗长,我的代码中需要大量的回调函数。有没有更优雅、更惯用的方法来解决这个问题?
(在 CoffeeScript 中使用 forEach 是我读过的不好的风格,但在我的实际代码中,我正在使用各种不能仅用 for 循环替换的 Leaflet 函数。)
【问题讨论】:
标签: coffeescript