Questions
11 of 19
1Explain the 5 rules that determine the value of this in JavaScript. Walk through each with an example.
2What is the default binding of this? How does strict mode change it?
3What is the difference between implicit and explicit binding? When does implicit binding get lost?
4How does this behave differently in arrow functions vs regular functions? Why?
5What does new binding do to this? What happens internally when you use new?
6Can this inside an arrow function be changed using .call(), .apply(), or .bind()? Why or why not?
7What is the value of this in a class constructor? What about in a class method called as a callback?
8What is the difference between call, apply, and bind? When would you use each?
9Explain how this works in event listeners. How do you preserve the correct this when using class methods as event handlers?
10How does this behave in a module (ESM) at the top level vs CommonJS?
11What is 'context loss' and what are the three most common ways to fix it in production code?
12How does Function.prototype.bind work under the hood? Can you implement a polyfill for it?
13How does this behave in prototype chain method calls? Does it refer to the instance or the prototype?
14When mixing ES6 classes with prototype manipulation, how can this become unpredictable?
15How do React class components use this, and why did binding in the constructor become a common pattern? How did arrow function class fields solve it?
16In what real-world scenarios have you encountered this-related bugs? How did you debug and fix them?
17What are the trade-offs between using arrow functions everywhere to avoid this confusion vs using regular functions with explicit binding?
18How would you explain the this keyword to a junior developer in one analogy?
19Can you think of a scenario where using .bind() causes a memory issue? How would you address it?
11 / 19

What is 'context loss' and what are the three most common ways to fix it in production code?

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);.

Difficulty: 5/10
Topics: this binding, arrow functions, callback context

Scenario Questions

0-2 years experience
  1. 1

    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?

  2. 2

    When adding a click listener with element.addEventListener('click', this.handleClick), why might this be undefined inside handleClick, and what simple change fixes it?

  3. 3

    If you call array.map(user.process), what problem can arise with this inside process, and how would you correct it?

2-5 years experience
  1. 1

    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?

  2. 2

    Our codebase has many .bind(this) calls, and linting suggests reducing them. What alternatives exist, and what are the readability and runtime trade‑offs?

  3. 3

    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.

5-8 years experience
  1. 1

    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.

  2. 2

    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?

  3. 3

    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.

8+ years experience
  1. 1

    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?

  2. 2

    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?

  3. 3

    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?

Follow-up Questions

  • How would you debug a situation where `this` is unexpectedly undefined?
  • What trade‑offs exist between using `.bind` and arrow functions?
  • In a performance‑critical path, which fix would you choose and why?