Skip to main content

Command Palette

Search for a command to run...

The Magic of this, call(), apply(), and bind() in JavaScript

Updated
β€’3 min read
The Magic of this, call(), apply(), and bind() in JavaScript

πŸ“Œ What Does this Mean in JavaScript?

Simple meaning:

πŸ‘‰ this means β€œwho is calling the function.”

It refers to the object that is executing the function.


πŸ“Œ 1️⃣ this Inside Normal Functions

Example:

function show() {
  console.log(this);
}

show();

In browser:

  • this β†’ window object

In strict mode:

  • this β†’ undefined

But don’t go deep right now.

Just remember:

πŸ‘‰ In a normal function, this depends on how the function is called.


πŸ“Œ 2️⃣ this Inside Objects

This is where it becomes useful.

let person = {
  name: "Rahul",
  greet: function() {
    console.log("Hi, I am " + this.name);
  }
};

person.greet();

Output:

Hi, I am Rahul

Why?

Because:

πŸ‘‰ person is calling greet()

So this = person object


πŸ“Œ What is call()?

call() lets you borrow a function and set this manually.


Example

let person1 = {
  name: "Rahul"
};

let person2 = {
  name: "Aman"
};

function greet() {
  console.log("Hello " + this.name);
}

greet.call(person1); // Hello Rahul
greet.call(person2); // Hello Aman

πŸ‘‰ call() sets who this should be.


call() with Arguments

function introduce(city) {
  console.log(this.name + " lives in " + city);
}

introduce.call(person1, "Bhopal");

Arguments are passed normally (comma separated).


πŸ“Œ What is apply()?

Same as call()

But arguments are passed as an array.


Example

introduce.apply(person1, ["Delhi"]);

Only difference:

  • call() β†’ arguments separated

  • apply() β†’ arguments inside array


πŸ“Œ What is bind()?

bind() does NOT call the function immediately.

It returns a new function with fixed this.


Example

let newFunction = introduce.bind(person2);

newFunction("Mumbai");

Output:

Aman lives in Mumbai

πŸ‘‰ bind() stores the connection for later use.


πŸ“Š Difference Between call, apply, bind

Method Calls Immediately? Arguments Format Returns New Function?
call() βœ… Yes Normal arguments ❌ No
apply() βœ… Yes Array ❌ No
bind() ❌ No Normal arguments βœ… Yes

πŸ“Š Diagram Idea – Function β†’ Caller Relationship

Function greet()
       ↑
   Called By
       ↑
   person object
       ↓
   this = person

πŸŽ“ Assignment Practice

1️⃣ Create Object with Method

let student = {
  name: "Aman",
  show: function() {
    console.log("Student: " + this.name);
  }
};

student.show();

2️⃣ Borrow Method Using call()

let student2 = { name: "Neha" };

student.show.call(student2);

3️⃣ Use apply()

function introduce(city) {
  console.log(this.name + " lives in " + city);
}

introduce.apply(student2, ["Jaipur"]);

4️⃣ Use bind()

let boundFunc = introduce.bind(student);

boundFunc("Indore");

🌟 Final Understanding

  • this = who is calling the function

  • call() = call now, set this

  • apply() = same as call, but array arguments

  • bind() = create new function, call later