call, apply, bind

_Khojiakbar_ - Jul 6 - - Dev Community

call

Why do we use call?
We use call when we want to borrow a function from one object and use it with another object.

What is call?
call is a way to use a function with a different object, pretending that object owns the function.

How does call work?
You take a function from one object and call it with another object, telling the function, "Hey, use this other object as your this."

Example:
Imagine you have two friends, Alice and Bob. Alice knows how to introduce herself, but Bob doesn't. You can help Bob introduce himself using Alice's way.

const alice = {
  name: 'Alice',
  introduce: function(greeting) {
    console.log(`${greeting}, my name is ${this.name}`);
  }
};

const bob = { name: 'Bob' };

alice.introduce.call(bob, 'Hello'); // Output: Hello, my name is Bob
Enter fullscreen mode Exit fullscreen mode

apply

Why do we use apply?
We use apply for the same reason as call, but when we want to pass arguments as an array.

What is apply?
apply is similar to call, but it takes arguments in an array instead of individually.

How does apply work?
You take a function from one object and call it with another object, giving the arguments as an array.

Example:
Imagine you want to introduce Bob, but this time you want to say both "Hello" and "Nice to meet you" at once.

const alice = {
    name: 'Alice',
    introduce: function(greetings) {
        const fullGreeting = [...greetings]
        console.log(`${fullGreeting[0]}. My name is ${this.name}. 
        ${fullGreeting[1]}`);
    }
  };

alice.introduce.apply(bob, ['Hello', 'Nice to meet you']); // Output: Hello, my name is Bob
Enter fullscreen mode Exit fullscreen mode

bind

Why do we use bind?
We use bind when we want to create a new function that always uses a specific object as this.

What is bind?
bind creates a new function that remembers which object should be this when it's called.

How does bind work?
You create a new version of a function that always uses the same this value.

Example:
Imagine you want to help Bob introduce himself later, but you want to make sure he does it the right way every time.

const bobIntroduce = alice.introduce.bind(bob);

bobIntroduce('Hi'); // Output: Hi, my name is Bob
bobIntroduce('Hey there'); // Output: Hey there, my name is Bob
Enter fullscreen mode Exit fullscreen mode

Summary with a Simple Story
Imagine you have a magical talking toy named Toy that can say things. Toy belongs to Alice, and it can say her name. You have another toy that belongs to Bob, but it doesn't know how to talk.

call: You tell Toy, "Say Bob's name using your voice," and it says, "Hello, my name is Bob."
apply: You tell Toy, "Say Bob's name and also 'Nice to meet you' using your voice," and it says, "Hello, my name is Bob. Nice to meet you."
bind: You create a new version of Toy that always knows it's talking about Bob. Whenever you tell this new toy to talk, it says, "Hi, my name is Bob."
These magical talking toys are just like functions in JavaScript, and call, apply, and bind help you control which toy is talking!

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player