Questions
1 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?
01 / 19

Explain the 5 rules that determine the value of this in JavaScript. Walk through each with an example.

The 5 rules for determining 'this' in JavaScript are: Default Binding (global/undefined), Implicit Binding (object context), Explicit Binding (call/apply/bind), 'new' Binding (constructor), and Arrow Function Binding (lexical scoping).

Default Binding: When a function is called standalone, this defaults to the global object (window in browsers, global in Node.js) in non-strict mode, or undefined in strict mode. Example: function show() { console.log(this); } show(); // global object or undefined.

Implicit Binding: When a function is called as a method on an object, this refers to the object the method is called on. Example: const obj = { name: 'Alice', greet() { console.log(this.name); } }; obj.greet(); // 'Alice'.

Explicit Binding: Using call, apply, or bind to directly set this. Example: function greet() { console.log(this.name); } greet.call({ name: 'Bob' }); // 'Bob'.

new Binding: When a function is called with the new keyword, this binds to the newly created object. Example: function Person(name) { this.name = name; } const p = new Person('Charlie'); console.log(p.name); // 'Charlie'.

Arrow Function Binding: Arrow functions do not have their own this; they inherit this from their enclosing lexical scope. Example: const obj = { name: 'Dave', greet: () => { console.log(this.name); } }; obj.greet(); // undefined (inherits from outer scope, likely window/global).

Difficulty: 5/10
Topics: implicit binding, explicit binding, lexical arrow binding

Scenario Questions

0-2 years experience
  1. 1

    If you call a plain function like function foo() { console.log(this); } foo(); what will be logged and why?

  2. 2

    When you invoke obj.method() where method is defined as function() { console.log(this); }, what does this refer to?

  3. 3

    Show how using foo.call(bar) changes the value of this inside foo.

2-5 years experience
  1. 1

    In a React class component, a click handler defined as handleClick() { console.log(this); } loses its this when passed to a button. Walk through why that happens and how you would fix it.

  2. 2

    You have a Node.js module that returns a function: module.exports = function() { return function() { console.log(this); }; };. When the inner function runs, this is undefined. Explain the rule causing this and how to bind it correctly.

  3. 3

    Why does using an arrow function inside setTimeout(() => { console.log(this); }, 0) give a different this than using a regular function, and which rule applies?

5-8 years experience
  1. 1

    You are building a utility library that supports both method chaining (obj.chain().next()) and standalone function calls. How would you design the API to ensure this is bound correctly in each usage pattern?

  2. 2

    When refactoring a large codebase to replace many traditional functions with arrow functions, what edge cases around this should you watch for to avoid subtle bugs?

  3. 3

    If you bind a method with fn = obj.method.bind(obj) inside a high‑frequency event loop, what are the performance considerations and are there alternatives?

8+ years experience
  1. 1

    Your organization is migrating a legacy front‑end that heavily relies on dynamic this binding to a more functional style. Outline a migration plan that minimizes regression risk related to this.

  2. 2

    In a micro‑frontend architecture, shared UI components may be rendered in different host applications. How would you guarantee correct this binding without leaking state between hosts?

  3. 3

    What long‑term coding guidelines would you establish across multiple teams to prevent this‑related bugs, especially when mixing class methods, arrow functions, and higher‑order functions?

Follow-up Questions

  • How does explicit binding interact with the new operator?
  • What are common bugs when an arrow function is used as an object method?
  • Can you describe a testing strategy to verify correct this binding in a module?