Fat arrow 'this' area

Consider this code

 _bindEvents: ->
  @input.bind 'keyup', =>
    @filter($(this).val())
    if $this .val() is ''
      @clearBtn.hide()
    else
      @clearBtn.show()

it’s clear to me that the “@” represents “this.” therefore, it refers to the parent area, but what if I need an “inner this”.

Like this line:

@filter($(this).val())

compiles:

_this.filter($(_this).val()); // $(_this)

and I need this:

_this.filter($(this).val());  // $(this)

Is there a way to do this without using the thin arrow and keeping this link manually using closure (that = this)?

+3
source share
2 answers

AFAIK there is no way to do this; and I would warn about this for several reasons:

  • ease of understanding: . When you use hash rockets ( =>), you effectively tell the reader what you need / want to keep the current value this; reintroduction of the second thisconfuses this.

  • :. , ECMAScript => , this. ( , CoffeeScript , )

, , .

+3

javascript:

`var second_this = this;`
@filter($(second_this).val())
0

All Articles