Nothing special with coffeescript. Just do
$link.click @_clicked
And then define _clicked
as
_clicked: (e) =>
# some code
It'll pass this._clicked
as the handler for the click event of link
.
You can pass any function as callbacks like this. The keypoint is to not execute the function (there are no () in @_clicked
). A function in JS is a normal variable, which can be passed around as parameters to other functions.
With
$link.click ->
youre just passing a function to .click
directly without storing it into a variable.
To keep the this
you either define @_clicked
with a fat arrow or you use JavaScripts bind
function which returns a function where this
is bound to a certain value.
CoffeeScript is not adding any extra functionality to JavaScript. The Fat arrow just does a bind similar to the bind
function from the link (if its used in a method definition of a class) or just does the var _this = this
trick when a function is defined inside a function.