Fixing Lost `this` Context in CoffeeScript Class Callbacks
When a CoffeeScript class method loses its instance context in a callback, `this` stops pointing to the instance. Use the fat arrow operator to bind it lexically.
23 Jun 2026, 06:35 UTC

The method runs, but `this` is not the instance
You pass a CoffeeScript class method to setTimeout, an event listener, or another higher-order function—a function that receives another function as an argument. When the callback executes, this refers to the global object, window, or undefined instead of the class instance. Reading this.property returns undefined, and the method throws or fails silently.
Useful takeaway: CoffeeScript's thin arrow (->) leaves this dynamic at call time. The fat arrow (=>) compiles the method into a closure that captures the surrounding this lexically—meaning the context is fixed by where the method is written, not by how it is invoked. This transform is stable in CoffeeScript 1.x and 2.x and does not require a runtime polyfill. If the method is used as a callback, declare it with =>.
Recognizable condition
- The method works when called directly on an instance.
- The same method fails when passed to
setTimeout,addEventListener, or a callback argument. - Inside the failing callback,
thisis the global object orundefined. - Instance properties accessed through
thisareundefined.
Cause and diagnostic table
| Observation | Likely cause | Confirmation check |
|---|---|---|
this is window or global in a setTimeout callback | Method declared with -> and passed as a callback | Compile the file and inspect for a missing _this = this capture |
this.property is undefined inside an event handler | Thin-arrow method detached from its instance by the caller | Call the method directly, then invoke it as a handler; compare this |
| Works when bound manually, fails after reassignment | Method replaced on the prototype or copied to another object | Check whether the method was reassigned after class definition |
Ordered checks
-
Check the arrow in the source
Open the CoffeeScript source file. Methods intended for callbacks should use
=>, not->. -
Compile to JavaScript and inspect
Run the CoffeeScript compiler where the source file is readable. No special permissions are required beyond file read access.
coffee -c your-class.coffeeExpected check: the compiled JavaScript for a fat-arrow method typically contains
_this = thisand an inner function referencing_this. A thin-arrow method usually does not.Risk: overwriting an existing compiled file. Compile to a temporary directory or inspect output before replacing build artifacts.
-
Run a runtime probe
Execute the compiled JavaScript in Node.js or a browser console.
node your-class.jsExpected check: a bound method logged inside an asynchronous callback shows the instance property. An unbound method shows
undefinedor the global object.Risk: browser globals differ from Node. Test in the same runtime where the failure occurs.
-
Check for reassignment and
superSearch for places where the method is copied, reassigned, or called through
super. Fat-arrow binding does not survive reassignment to another object, andsuperis not automatically bound.
Fixes tied to findings
Finding: method uses ->
Change the declaration to =>.
class Worker
constructor: (@name) ->
# Thin arrow: this is dynamic at call time
report: -> console.log @name
# Fat arrow: this is captured lexically
reportBound: => console.log @name
Pass reportBound to setTimeout or an event listener. The compiled wrapper preserves the instance context.
Finding: method must remain thin but needs binding in one place
Bind at the call site or in the constructor rather than changing the method declaration.
class Worker
constructor: (@name) ->
@boundReport = => console.log @name
report: -> console.log @name
This creates a bound function instance per object, which increases memory compared with a prototype method.
Finding: method is reassigned or used with super
Do not rely on => to bind methods that are copied to another object or invoked through super. Capture this explicitly in the constructor, or restructure so the callback receives the instance as an argument instead of relying on this.
Limitations
- Each class instance gets its own function instance for every fat-arrow method, increasing memory usage compared with thin-arrow prototype methods.
=>capturesthisonly. It does not bind arguments, and it does not bindsuper.- Reassigning a bound method to another object breaks the captured context.
- Mixins or decorators that replace methods on the prototype may not interact predictably with constructor-bound functions.
Practical verification
Create a small CoffeeScript class with one thin-arrow method and one fat-arrow method. Compile it, then pass both to setTimeout.
class Example
constructor: (@value) ->
thin: -> console.log "thin:", this, @value
fat: => console.log "fat:", this, @value
inst = new Example 42
setTimeout inst.thin, 0
setTimeout inst.fat, 0
Expected behavior: the fat-arrow callback logs the instance and 42. The thin-arrow callback logs the global object or undefined and undefined for the value. Confirm this in the runtime where the original failure occurred, not only in Node if the issue appears in a browser.
Escalation criteria
- Switching to
=>fixes the symptom but many instances cause measurable memory pressure. - The application needs
superinside bound callbacks. - Methods are frequently reassigned or mixed into other objects.
- The team is maintaining legacy CoffeeScript 1.x code and cannot rely on the
=>transform behaving identically across all build tools.
In those cases, prefer passing the instance explicitly to callbacks or refactoring to plain functions that receive context as an argument, rather than depending on bound method syntax.