In a fat arrowed function of a coffeescript class, how can I access the scope of the class as well as the function?
Example:
class Example
foo: ->
$('.element').each => # or ->
@bar($(this)) # I want to access 'bar' as well as the jquery element
bar: (element) ->
element.hide()
So in this example, if I use a => then the @ refers to the this of the class but the ‘this’ is then wrong, whereas if I use a -> for the each, then the ‘this’ is correctly scoped but but then how do I reference the class function bar?
Thanks!
That’s because in CoffeeScript
@is an alias forthisi.e. when you compile your .coffee to .js@will be replaced withthis.If
Example::baris ugly, I don’t think there are ‘prettier’ solutions.You can store a reference to
thisbefore calling.each: