CoffeeScript Fat Arrow: Keep `this` Bound in Callbacks
CoffeeScript's `=>` fat arrow captures `this` lexically, preventing lost instance context when methods are passed as callbacks. Learn when to use it and how to verify the compiled output.
31 May 2026, 02:04 UTC

The callback that lost its instance
You write a CoffeeScript class to manage a UI widget. One method handles a button click, so you pass it to addEventListener or a jQuery handler. When the button is clicked, the method runs, but this inside it refers to the DOM element—not your class instance. Properties like @element or @config are undefined, and the handler throws.
This happens because CoffeeScript’s thin arrow, ->, compiles to a regular JavaScript function. In JavaScript, this inside a plain function is determined by how the function is called, not where it is defined. When a library invokes your method as a callback, it rebinds this to its own context.
What the fat arrow actually does
CoffeeScript’s fat arrow, =>, compiles to a JavaScript closure that captures this from the surrounding scope at definition time. The compiler typically emits a pattern like var _this = this; and references _this inside the generated function. Because the reference is captured lexically, the callback keeps the original instance even when another object invokes it.
This is the same semantic as ES6 arrow functions in modern JavaScript. If you are compiling CoffeeScript to ES5 for older browsers, the fat arrow is the practical way to get that behavior without writing closures by hand.
Worked example: a button handler in a class
Create a file named widget.coffee:
class Widget
constructor: (@element) ->
@element.addEventListener 'click', @handleClick
handleClick: (event) =>
console.log @element.id
console.log @config
logDirect: ->
console.log @element.id
Run these commands in your terminal. No special permissions are required:
coffee -v
coffee --compile --bare widget.coffee
The -v flag prints the compiler version. Emitted JavaScript patterns can vary between releases, so note the version before you inspect output. The compile command produces widget.js in the same directory.
Open widget.js and look for the closure pattern around handleClick. You should see something equivalent to:
var _this = this;
// ...
handleClick: function(event) {
return console.log(_this.element.id);
}
That _this assignment is the signal that the fat arrow captured the instance. By contrast, logDirect, defined with ->, compiles to a plain function without the capture.
To verify at runtime, run the compiled file in Node or serve it in a browser and trigger the click. Inside handleClick, @element should resolve to the instance’s element, not the event target. If you see undefined or the wrong object, the binding was not applied.
Trade-offs and limitations
- Fat-arrow functions cannot be used as constructors with
new. - They do not have their own
argumentsobject; they capture the enclosing scope’sarguments. - Mixing
->and=>in the same class can cause subtle bugs if you extract a method and expect dynamicthis. - Compiler versions may emit different ES5 helper patterns. Inspect the generated JavaScript rather than assuming it is identical across releases.
Use -> for methods called directly on an instance, where dynamic this is intentional. Reserve => for methods passed as callbacks to DOM APIs, timers, or third-party libraries.
Verification checklist
- Check the compiler version:
coffee -v - Compile your file:
coffee --compile --bare <file>.coffee - Inspect the output for
_this = thisnear fat-arrow methods. - Run the compiled code and confirm
thisrefers to the instance inside the callback.
If the compiled output lacks the capture pattern, or if this is wrong at runtime, switch the method to => and recompile. Changing a method from -> to => alters binding behavior, so test any callers that relied on the old dynamic context.
The practical rule is short: define instance methods with ->, and define callbacks with =>. When you hand a method to another API, the fat arrow keeps this pointed at your instance.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.