Reputation: 455
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 '@' represents '_this.' so it references the parent scope, but what if i need the 'inner this'.
Like this line:
@filter($(this).val())
compiles to this:
_this.filter($(_this).val()); // $(_this)
and I need this:
_this.filter($(this).val()); // $(this)
Is there a way to do that without using the thin arrow and saving the this reference by hand using a closue (that = this)?
Upvotes: 3
Views: 1237
Reputation: 2023
You can always hack this using embedded javascript:
`var second_this = this;`
@filter($(second_this).val())
Upvotes: 0
Reputation: 8101
AFAIK there's no way to do that; and I would warn against it for a couple reasons:
ease-of-understanding: When you use a hash rocket (=>
), you're effectively telling the reader that you need/want to preserve the current value of this
; re-introducing a second this
confuses that.
future-compatibility: From my understanding, the next ECMAScript spec is to support =>
in a way that it doesn't even introduce a new this
. (And I wouldn't be surprised if CoffeeScript directly adopts the new
arrow syntax when that feature lands)
A thin arrow w/ explicit referencing will probably make things more clear in the end.
Upvotes: 3