SkillPilotAI

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.
JavaScript
const user = {  name: "Rahul",  greet() {    console.log(`Hi, ${this.name}`);  },}; user.greet(); const greet = user.greet;greet();
Output
Hi, RahulTypeError: Cannot read properties of undefined (reading 'name')
Why this became undefined
  1. 1.user.greet() -> object before the dot -> this = user

  2. 2.const greet = user.greet -> only the function is copied

  3. 3.greet() -> plain call, no object

  4. 4.Strict mode / module -> this = undefined

  5. 5.undefined.name -> TypeError

this is decided at call time, not at definition time

11.2 The 4 binding rules (priority order)

PriorityHow it is calledthis becomesExample
1. new bindingWith the new keywordA brand new objectnew User("Rahul")
2. Explicit bindingcall / apply / bindThe object you passedintro.call(rahul)
3. Implicit bindingThrough an object: obj.method()That objectuser.greet()
4. Default bindingPlain call: fn()undefined (strict/module), window in old non-module scriptsgreet()
Deciding the value of this
  1. 1.Called with new? -> this = new object

  2. 2.Else, called via call / apply / bind? -> this = passed object

  3. 3.Else, called as obj.method()? -> this = obj

  4. 4.Else, plain call -> this = undefined (strict)

  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.
JavaScript
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"]);
Output
Rahul Delhi DevPriya Pune QA

11.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).
JavaScript
const user = {  name: "Rahul",  greet() {    console.log(`Hi, ${this.name}`);  },}; const greet = user.greet.bind(user);greet(); setTimeout(greet, 1000);
Output
Hi, RahulHi, Rahul

11.5 Comparison: call vs apply vs bind

Pointcallapplybind
Runs the functionImmediatelyImmediatelyNo, returns a new function
ArgumentsComma separatedOne arrayComma separated (can be pre-filled)
Return valueFunction's resultFunction's resultBound function
Typical useBorrow a method onceArguments already in an arrayCallbacks, 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.
JavaScript
const cart = {  owner: "Rahul",  items: ["Shoes", "Watch"],  list() {    this.items.forEach((item) => {      console.log(`${this.owner}: ${item}`);    });  },}; cart.list();
Output
Rahul: ShoesRahul: Watch
JavaScript
const rahul = {  name: "Rahul",  getGreet() {    return () => {      console.log(`Hi, ${this.name}`);    };  },}; const greet = rahul.getGreet();greet.call({ name: "Priya" });
Output
Hi, Rahul

11.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.
JavaScript
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();
Output
Hi, Rahul

11.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.
JavaScript
// 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.