我注意到,如果我尝试像这样编译 Coffeescript 行:
$note.find('a.close').bind 'click', (event) =>
$(this).parent().remove()
$(this)
编译为$(_this)
,考虑到我使用的是=>
. 问题是,我不想->
在以后需要在这个范围内使用局部变量时使用。
我必须用反引号转义第二行才能$this
正确编译,à la:
`$(this).parent().remove()`
…或者,还有更好的方法?
更新(2012 年 7 月 12 日):
我最终这样做了:
close = -> $note.remove()
$note.find('a.close').bind 'click', (event) ->
close()
setTimeout close, duration
我知道它this
完全避免了使用,但这似乎是一个比使用self = this
普通 JS 开发通常需要的相同技巧更清洁的解决方案。