this, call/apply/bind
The value of this is decided by how a function is called, not where it is written. That one rule explains why a method works on user.greet() but throws a TypeError once you store it in a variable, and it is one of the most asked JavaScript interview topics.
11.1 The problem: this got lost
- user.greet() works because there is an object and a dot before the call, so this = user.
- const greet = user.greet copies only the function, not the object it came from.
- greet() is a plain call with no object, so in modules / strict mode this = undefined.
- Reading undefined.name throws a TypeError.
const user = { name: "Rahul", greet() { console.log(`Hi, ${this.name}`); },}; user.greet(); const greet = user.greet;greet();Hi, RahulTypeError: Cannot read properties of undefined (reading 'name')- Step 1
1.user.greet() -> object before the dot -> this = user
- Step 2
2.const greet = user.greet -> only the function is copied
- Step 3
3.greet() -> plain call, no object
- Step 4
4.Strict mode / module -> this = undefined
- Step 5
5.undefined.name -> TypeError
this is decided at call time, not at definition time
11.2 The 4 binding rules (priority order)
| Priority | How it is called | this becomes | Example |
|---|---|---|---|
| 1. new binding | With the new keyword | A brand new object | new User("Rahul") |
| 2. Explicit binding | call / apply / bind | The object you passed | intro.call(rahul) |
| 3. Implicit binding | Through an object: obj.method() | That object | user.greet() |
| 4. Default binding | Plain call: fn() | undefined (strict/module), window in old non-module scripts | greet() |
- Step 1
1.Called with new? -> this = new object
- Step 2
2.Else, called via call / apply / bind? -> this = passed object
- Step 3
3.Else, called as obj.method()? -> this = obj
- Step 4
4.Else, plain call -> this = undefined (strict)
- Step 5
5.Arrow function? Skip all rules -> this from parent scope
11.3 call and apply
- Explicit binding: you decide what this will be.
- call(thisArg, a, b): first argument becomes this, the rest go one by one separated by commas.
- apply(thisArg, [a, b]): same job, but the arguments go inside an array.
- Both call the function immediately.
function intro(city, role) { console.log(this.name, city, role);} const rahul = { name: "Rahul" };const priya = { name: "Priya" }; intro.call(rahul, "Delhi", "Dev");intro.apply(priya, ["Pune", "QA"]);Rahul Delhi DevPriya Pune QA11.4 bind: the fix for lost this
- bind does not call the function; it returns a new function with this locked forever.
- The bound function works even when called alone or passed as a callback (setTimeout, event handlers).
- Memory trick: call = Comma, apply = Array, bind = later (Baad me).
const user = { name: "Rahul", greet() { console.log(`Hi, ${this.name}`); },}; const greet = user.greet.bind(user);greet(); setTimeout(greet, 1000);Hi, RahulHi, Rahul11.5 Comparison: call vs apply vs bind
| Point | call | apply | bind |
|---|---|---|---|
| Runs the function | Immediately | Immediately | No, returns a new function |
| Arguments | Comma separated | One array | Comma separated (can be pre-filled) |
| Return value | Function's result | Function's result | Bound function |
| Typical use | Borrow a method once | Arguments already in an array | Callbacks, setTimeout, event handlers |
11.6 this in arrow functions
- Arrow functions have no this of their own; they take it from the parent scope (lexical this), like closures take variables.
- An arrow inside forEach in a method gets the method's this, so this.owner works.
- A normal function callback there would be a plain call and this would be undefined.
- call / apply / bind cannot change an arrow's this; it is fixed when the arrow is created.
const cart = { owner: "Rahul", items: ["Shoes", "Watch"], list() { this.items.forEach((item) => { console.log(`${this.owner}: ${item}`); }); },}; cart.list();Rahul: ShoesRahul: Watchconst rahul = { name: "Rahul", getGreet() { return () => { console.log(`Hi, ${this.name}`); }; },}; const greet = rahul.getGreet();greet.call({ name: "Priya" });Hi, Rahul11.7 Checkpoint: bind beats implicit binding
- hi is bound to rahul, then attached as a method of priya.
- priya.hi() looks like implicit binding (Hi, Priya), but the bound this is permanent.
- Explicit (bind) has higher priority than implicit, so the answer is Hi, Rahul.
const rahul = { name: "Rahul" };const priya = { name: "Priya" }; function hi() { console.log(`Hi, ${this.name}`);} const hiRahul = hi.bind(rahul);priya.hi = hiRahul;priya.hi();Hi, Rahul11.8 Common mistakes
- Writing an object method as an arrow function: greet: () => {...} uses the outer this (undefined at module top level) and throws a TypeError. Use greet() {...}.
- Passing a method as a callback (setTimeout(user.greet)) without bind or an arrow wrapper.
- Believing call or bind can change the this of any function: arrow functions ignore them.
- Expecting this to be window everywhere: in modules and strict mode a plain call gives undefined.
// Wrong: TypeErrorconst user = { name: "Rahul", greet: () => { console.log(`Hi, ${this.name}`); },}; // Right: Hi, Rahulconst user2 = { name: "Rahul", greet() { console.log(`Hi, ${this.name}`); },};11.9 Best practices
- Object methods: normal method syntax; callbacks inside those methods: arrow functions.
- Passing a method somewhere? bind it or wrap it: setTimeout(() => user.greet(), 1000).
- If a function does not need this, avoid this and pass values as arguments.
11.10 Interview tips
- Open with: the value of this is decided by how a function is called.
- List the rules in priority order: new, explicit (call/apply/bind), implicit (obj.method()), default (undefined in strict mode).
- Arrow functions have no own this; they use the parent scope's this and call/bind cannot change it.
- call = comma, apply = array, bind = returns a function for later.
- Mention the classic bug: a method passed as a callback loses this; fix with bind or an arrow wrapper.