【发布时间】:2011-10-19 23:09:22
【问题描述】:
在咖啡脚本类的胖箭头函数中,如何访问该类的范围以及函数?
例子:
class Example
foo: ->
$('.element').each => # or ->
@bar($(this)) # I want to access 'bar' as well as the jquery element
bar: (element) ->
element.hide()
所以在本例中,如果我使用 =>,那么 @ 指的是类的 this,但 'this' 是错误的,而如果我对 each 使用 ->,那么 'this' 是范围正确,但是如何引用类函数栏?
谢谢!
【问题讨论】:
-
我找到了一个临时解决方案:使用 -> 然后使用 Example::bar 引用 bar。它有效,但它很丑。谁有更漂亮的解决方案?
标签: coffeescript scope arrow-functions