Context loss occurs when a function loses its intended this binding, often when passing a method as a callback. The three common fixes are: using .bind(), arrow functions, or storing this in a variable (e.g., const self = this).
Context loss happens when a method is detached from its object, causing this to revert to default binding (global or undefined). This frequently occurs with callbacks. The three most common production fixes are: (1) .bind() — explicitly bind the method to the object: setTimeout(this.method.bind(this), 100); (2) Arrow functions — capture lexical this: setTimeout(() => this.method(), 100); (3) Store this in a variable — pre-ES6 pattern: const self = this; setTimeout(function() { self.method(); }, 100);.
You have an object with a method log() and you pass obj.log to setTimeout. What will this be inside log, and how can you make it refer to obj?
When adding a click listener with element.addEventListener('click', this.handleClick), why might this be undefined inside handleClick, and what simple change fixes it?
If you call array.map(user.process), what problem can arise with this inside process, and how would you correct it?
During a refactor a developer replaced a regular method with an arrow function inside a class. What side effects could that have on this binding, and how would you decide which version to keep?
Our codebase has many .bind(this) calls, and linting suggests reducing them. What alternatives exist, and what are the readability and runtime trade‑offs?
We observed a memory leak after adding many callbacks that capture this. Explain why context loss might be contributing and which fix (bind, arrow, or self variable) is most appropriate for performance.
Our front‑end framework creates thousands of component instances that each register event listeners using class methods. Occasionally this is lost after hot‑module replacement. Propose a strategy to manage context consistently across the component hierarchy.
We are migrating a large legacy codebase to TypeScript and want to eliminate context‑loss bugs. What architectural patterns or tooling can enforce correct binding, and how would you roll them out?
In a high‑throughput Node.js service we use many callbacks. Discuss the performance implications of .bind versus arrow functions versus storing self, and recommend the best practice for this environment.
Our organization is standardizing a shared UI library used by multiple teams. How would you design the library’s API to prevent context‑loss errors for consumers, and what guidelines would you enforce?
When planning a migration from callback‑based code to async/await, how do you address existing context‑loss issues and ensure future code avoids them across teams?
In a micro‑frontend architecture where each micro‑app may use different bundlers, what cross‑team conventions or build‑time checks could catch context‑loss bugs before deployment?