Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Not sure that I understand you. Do you mean that you find it odd to use =>, and thus resort to self=@ for your callbacks?

Also, sometimes I do

    $.each lst, (i, item) ->
      whatever x
because I always use jQuery in my projects.


I often have to resort to self = @ when using jQuery in classes.

Because of how jQuery binds the queried element to this, and the fat arrow overrides this, there doesn't seem to be another way to access both your class and the jQuery element in the same function.

for instance:

    class ButtonLogger
    
      constructor: ->
        @clickedButtons = []
        
        $(document).ready => @init()

      init: ->
        self = @

        $('button').click ->
          $button = $(@)
          self.clickedButtons.push $button.attr 'id'


Try `event.currentTarget`, and `event.target` instead. They're much preferable to having a `self` (representing some random DOM element), and a `this` within a function.

    $('button').click (e) =>
      @clickedButtons.push $(e.target).attr 'id'




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: