如何在jQuery $.bind()中使用Coffeescript’this’?

前端之家收集整理的这篇文章主要介绍了如何在jQuery $.bind()中使用Coffeescript’this’?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在Coffeescript中使用课程时,我遇到了一个重大问题,让我举例说明一下
  1. class Bet
  2. constructor: () ->
  3.  
  4. placeBet: ->
  5. $('#chips > div').bind 'click',->
  6. amount = $(this).attr 'id'
  7. pile = $(this)
  8. switch amount
  9. when "ten" then this.bet pile,amount #This line causes a problem
  10.  
  11. bet: (pile,amount) ->
  12. alert 'betting!'

对this.bet的调用会产生以下错误

未捕获TypeError:Object#没有方法’bet’

所以,目前我的类的实例方法没有被调用,
如何正确地调用我的类的bet方法,而不会与jQuery选择器的这种碰撞(这是我以为现在发生了什么)?

提前非常感谢!

解决方法

尝试这个:
  1. class Bet
  2. constructor: () ->
  3.  
  4. placeBet: ->
  5. that = this
  6. $('#chips > div').bind 'click',->
  7. amount = $(this).attr 'id'
  8. pile = $(this)
  9. switch amount
  10. when "ten" then that.bet pile,amount) ->
  11. alert 'betting!'

猜你在找的jQuery相关文章