Call, Apply, and Bind in JavaScript
In JavaScript, call()
, apply()
, and bind()
are methods that allow you to control the value of this
when calling a function. They are especially useful when working with objects and borrowing methods.
1. call()
Method
The call()
method invokes a function immediately with a specific value for this
, and you can pass arguments individually.
Syntax:
javascriptCopyEditfunctionName.call(thisArg, arg1, arg2, ...)
Example:
javascriptCopyEditconst person = {
name: 'Alice',
greet: function (city) {
console.log(`Hello, my name is ${this.name} and I live in ${city}`);
}
};
const person2 = { name: 'Bob' };
person.greet.call(person2, 'New York');
// Output: Hello, my name is Bob and I live in New York
Key Points:
call()
calls the function immediately.thisArg
is the value ofthis
inside the function.- Arguments are passed individually.
2. apply()
Method
The apply()
method is similar to call()
, but it takes arguments as an array instead of individual values.
Syntax:
javascriptCopyEditfunctionName.apply(thisArg, [arg1, arg2, ...])
Example:
javascriptCopyEditconst person = {
name: 'Alice',
greet: function (city, country) {
console.log(`Hello, my name is ${this.name} and I live in ${city}, ${country}`);
}
};
const person2 = { name: 'Bob' };
person.greet.apply(person2, ['Los Angeles', 'USA']);
// Output: Hello, my name is Bob and I live in Los Angeles, USA
Key Points:
apply()
calls the function immediately.- Arguments are passed as an array.
- Useful when you don’t know the number of arguments in advance (e.g.,
Math.max.apply(null, [1, 2, 3])
).
3. bind()
Method
The bind()
method returns a new function with a specific value for this
, but does not call the function immediately.
Syntax:
javascriptCopyEditconst boundFunction = functionName.bind(thisArg, arg1, arg2, ...)
Example:
javascriptCopyEditconst person = {
name: 'Alice',
greet: function (city) {
console.log(`Hello, my name is ${this.name} and I live in ${city}`);
}
};
const person2 = { name: 'Bob' };
const boundGreet = person.greet.bind(person2, 'Chicago');
boundGreet(); // Output: Hello, my name is Bob and I live in Chicago
Key Points:
bind()
does not call the function immediately.- Returns a new function with
this
permanently set tothisArg
. - Useful when passing functions as callbacks or preserving
this
context in asynchronous operations.
Key Differences Summary:
Method | Calls the Function Immediately? | Arguments Passed Individually or as an Array? | Returns a New Function? |
---|---|---|---|
call() | Yes | Individually | No |
apply() | Yes | As an array | No |
bind() | No | Individually | Yes |
When to Use Which?
Use Case | Recommended Method |
---|---|
Call a function immediately with a specific this | call() or apply() |
Pass arguments as an array | apply() |
Create a new function with this bound for later execution | bind() |
Event handlers, callbacks that need a fixed this | bind() |
Practical Example Combining All:
javascriptCopyEditconst person = {
name: 'Alice',
age: 25
};
function introduce(city, country) {
console.log(`Hi, I'm ${this.name}, ${this.age} years old, from ${city}, ${country}`);
}
// call
introduce.call(person, 'New York', 'USA');
// apply
introduce.apply(person, ['London', 'UK']);
// bind
const boundIntroduce = introduce.bind(person, 'Paris', 'France');
boundIntroduce();
Output:
rustCopyEditHi, I'm Alice, 25 years old, from New York, USA
Hi, I'm Alice, 25 years old, from London, UK
Hi, I'm Alice, 25 years old, from Paris, France