CoffeeScript: How to use both fat arrow and this?

后端 未结 4 1262
既然无缘
既然无缘 2020-12-25 11:55

I have a coffeescript class that has some jquery event listeners. I would like to use the fat arrow => to avoid having to reference the class, but I still ne

4条回答
  •  一向
    一向 (楼主)
    2020-12-25 12:41

    CoffeeScript links both this and @ to the outer context, therefore you cannot access the context that jQuery provided (aka the desired "this"). Use event.target instead:

    class PostForm
        constructor: ->
            $('ul.tabs li').on 'click', (event) =>
                tab = $(event.target)
                @highlight_tab(tab)
    

提交回复
热议问题