All 35 Bugs /
0/35 Fixed ✓
JavaScriptEasy~8 min

this Context Lost When Passing Object Method as Callback

What's Broken (Symptom)

Passing `counter.increment` directly as a button callback sets `this` to the button DOM element (or `undefined`), returning `NaN`.

Expected Behavior

  • • Preserve `counter` `this` binding via `.bind(counter)` or an arrow wrapper `() => counter.increment()`.
  • • Clicking `[data-testid="inc-btn"]` increments `counter.count` cleanly.

Debugging Hints (0/2 used)

Try spotting the bug in the editor first. Need a nudge? Reveal Hint 1 anytime.

Root Cause Breakdown

Pass all 1 automated verification checks on the right to unlock the full Root Cause explanation.

Automated Bug Verification

1 Checks Ready
  • Preserves counter.count instead of NaNClicking button twice sets counter.count to 2 and DOM text to "2".
bugfix.html
Live Sandboxed PreviewCtrl + Enter to verify
Console Output0 log(s)
No console output yet. Run Preview or Verify Bug Fix.