【发布时间】:2014-07-03 04:45:15
【问题描述】:
我想知道是否有可能以某种方式阻止 this 关键字在胖箭头回调 (=>) 中转换为 _this?
例如:
class someClass
someMethod: ->
$(document).on 'click', '.myclass', (e) =>
# doing things with right context this, it's ok
@anotherMethod()
@oneMoreMethod()
# but here I need jQuery ``this`` pointing to element
$el = $ this # this is transformed into ``_this`` :(
也许我错过了一些选项或运算符?
更新
我知道self = this 之类的技巧,但我认为 CS 有更优雅的东西..
【问题讨论】:
-
尝试使用
$(e.target) -
-
一个奇怪的技巧是用
<backtick><space>this<backtick>替换this,它使用正常的this。第一个反引号和 this 之间需要空格。看到它here -
没有人欠你一个不赞成的评论。有人认为您的问题“不清楚或无用”,或者缺乏研究。可能是因为你的问题没有意义。您在问如何防止
=>明确设计给您的单一、孤独的行为。如果您不希望这种行为,请使用->。 -
@vcsjones 当 CoffeeScript 编译器更新时,这种诡计几乎肯定会失败。依赖错误和怪癖是一个非常糟糕的主意,当有合理的解决方案时更是如此。
标签: javascript jquery coffeescript this jquery-callback