Javascript Coffeescript对象、jquery回调和变量范围冲突和混淆

Javascript Coffeescript对象、jquery回调和变量范围冲突和混淆,javascript,jquery,coffeescript,Javascript,Jquery,Coffeescript,试图找到完成此工作的最佳方法: class Person constructor: (@el) -> @el = $(el) this.bind() bind: -> @el.find('.something').on 'click', -> $(this).hide() # conflict! this.do_something() # conflict! do_something: ->

试图找到完成此工作的最佳方法:

class Person

  constructor: (@el) ->
    @el = $(el)
    this.bind()

  bind: ->
    @el.find('.something').on 'click', ->
      $(this).hide()       # conflict!
      this.do_something()  # conflict!

  do_something: ->
    alert 'done!'

我知道我可以使用散列火箭(=>),然后访问
这个。从我的回调中执行一些
操作,但这与
回调“this”
冲突,因此jquery正在尝试选择对象,而不是
元素。某些东西“
。如何解决此问题?

您无法使
引用不同的对象。通过将实例的
引用存储在辅助变量中,使用不同的标识符:

  bind: ->
    person = this
    @el.find('.something').on 'click', ->
      $(this).hide()
      person.do_something()

那是最好的办法吗?顺便说一句,谢谢@Zenph是的,这是最好的(可读性最好的)方法。您不能使用
指向同一上下文中的不同对象。我理解上下文;)我们希望CS或jQuery为这种冲突提供一个代理变量。