Fix lost this binding in callback registration

intermediate
By FrontendAtlas Editorial · Updated Oct 5, 2026
A teammate passes an object method straight into a callback API, assuming it will still remember its owner later. Direct calls like tracker.recordClick() work, but the same method breaks once another function invokes it without the original receiver. Fix the bug so registered callbacks keep the right this.
Examples
const tracker = createClickTracker('save');
tracker.recordClick(); // 'save:1'
tracker.attach(handler => handler()); // 'save:2'

const tracker = createClickTracker('open');
tracker.attach(handler => [handler(), handler()]); // ['open:1', 'open:2']