this Binding Rules
Understand how JavaScript decides what this means at the moment a function is called.
Learn new, call, apply, bind, implicit and default binding, arrow-function lexical this, callbacks, classes, DOM events, React, and interview traps.
The call-site rule
For a regular function, this usually refers to the object associated with the current function call. The most important idea is that this is determined by how the function is called, not where it is written.
call style → binding rule → value of this
Arrow functions are the exception: they capture this lexically from the surrounding scope.
function User(name) {
this.name = name;
}
const user = new User("Zubair");At this call site, this refers to The newly created instance.
introduce.call(user, "Engineer");
introduce.apply(user, ["Engineer"]);
const fixed = introduce.bind(user);At this call site, this refers to The object supplied explicitly.
const cart = {
items: 3,
count() { return this.items; }
};
cart.count(); // 3At this call site, this refers to The object before the dot.
"use strict";
function showThis() {
console.log(this);
}
showThis(); // undefinedAt this call site, this refers to undefined in strict mode.
Choose a call style and see which binding rule determines this.
| Method | Executes immediately | Arguments | this |
|---|---|---|---|
| call() | Yes | Individually | Supplied object |
| apply() | Yes | Array | Supplied object |
| bind() | No | Individually | Permanently bound object |
| obj.method() | Yes | Normal call | Object before the dot |
| arrow function | Depends on call | Normal arguments | Lexical outer this |
const user = {
name: "Zubair",
greet() { console.log(this.name); }
};
const greetUser = user.greet;
greetUser(); // undefined in strict modeExtracting a method turns the call into a plain function call. Use user.greet.bind(user) or a wrapper arrow function.
const user = {
name: "Zubair",
greetLater() {
setTimeout(() => console.log(this.name), 1000);
}
};The arrow callback captures this from greetLater, where the method was called as user.greetLater().
button.addEventListener("click", event => {
console.log(event.currentTarget);
});event.currentTarget is clearer than relying on this in a DOM handler.
button.addEventListener(
"click",
() => checkoutService.placeOrder(order)
);A wrapper arrow closes over checkoutService and avoids passing an unbound method.
Best Practices and Interview Answer
30-second answer: this is usually determined by the call site. new uses the new instance, call/apply/bind set it explicitly, obj.method() uses obj, and a plain call is undefined in strict mode. Arrow functions do not have their own this; they capture it lexically.
this Binding Quiz
Select an answer to see the explanation.
1. What usually determines the value of this for a regular function?
2. What does this refer to inside a function called with new?
3. Which methods provide explicit binding?
4. In user.greet(), what is this for a regular method?
5. What happens when an object method is extracted and called as greetUser()?
6. How do arrow functions determine this?
7. Why are arrow functions useful inside object-method callbacks?
8. What is the preferred value for a DOM event target?
9. Can bind() replace the this of an arrow function?
10. Which fix safely passes an object method as a callback?
Score: 0 / 10