JavaScript Function methods - call(), apply(), bind()
From Borrowing to Binding: The Art of Function Invocation in JavaScript
Every function in JavaScript is a Function object.
Function object
Function objects, they come equipped with powerful built-in methods such as call(), apply(), and bind()— each of which lets you explicitly control how and where a function is executed, and more importantly, what this refers to within it.We shall discuss three of its methods -
call()
apply()
bind()
call()
Syntax
call(thisArg, arg1, ... , argN)
Parameters
thisArg: Object to be used as current object
arg1, ... , argN: Comma-separated list of arguments to be passed to the function
Example
Let's dissect the call() method using a simple example.
Consider an object person that has a property name and a method feedDog
const person = {
name: "Sally",
feedDog() {
console.log(this.name, "feeds the dog");
}
}
Consider another object neighbor with a property name. Note that neighbor does not feed the dog and hence, it has no method called feedDog
const neighbor = {
name: "Jon",
}
Now, let's say on a certain day person is not available and so asks her neighbor to feed the dog.
neighbor has no method called feedDog so it borrows person's feedDog method
person.feedDog.call(neighbour)
// Jon feeds the dog
The above line of code calls person.feedDog on the the neighbor object. So this now refers to the neighbor object so this.name resolves to Jon
The params following thisArg are arguments to the function.
Let's modify the person.feedDog to accept two arguments:
const person = {
name: "Sally",
feedDog(numberOfBowls) {
console.log(this.name, "feeds the dog", foodItem, "at", time);
}
}
person.feedDog.call(neighbour, "chimken", "12 noon")
// Jon feeds the dog chimken at 12 noon
In summary, the call() method lets you borrow functions from one object and use them with another by explicitly setting what this refers to.
It executes the function immediately and accepts arguments individually rather than as an array.
apply()
The apply() method is similar to call(), except that it takes arguments as an array (or array-like object) rather than a comma-separated list.
Syntax
apply(thisArg, [argsArray])
Parameters
thisArg: Object to use as the current object (this) inside the function.argsArray: Array (or array-like object such asarguments) containing arguments to pass to the function.
Example
Let’s use the same setup:
const person = {
name: "Sally",
feedDog(foodItem, time) {
console.log(this.name, "feeds the dog", foodItem, "at", time);
}
}
const neighbor = {
name: "Jon"
};
Now, instead of calling with individual arguments, we’ll use apply() with an array:
person.feedDog.apply(neighbor, ["chimken", "12 noon"]);
// Jon feeds the dog chimken at 12 noon
The result is identical to call(), but the way arguments are passed is different.
When to use apply()
When your arguments are already in an array form.
When you don’t want to manually unpack or spread them.
A common use case is when using built-in functions like Math.max:
const numbers = [3, 5, 8, 2];
const max = Math.max.apply(null, numbers);
console.log(max); // 8
bind()
While call() and apply() invoke the function immediately, bind() returns a new function with a permanently bound this value.
Syntax
bind(thisArg, arg1, ..., argN)
Parameters
thisArg: The object to be used asthiswhen the function is called.arg1, ..., argN: Optional arguments to pre-set (partial application).
Example
const person = {
name: "Sally",
feedDog(foodItem, time) {
console.log(this.name, "feeds the dog", foodItem, "at", time);
}
}
const neighbor = { name: "Jon" };
// Bind returns a new function
const feedByNeighbor = person.feedDog.bind(neighbor, "chimken", "12 noon");
// The function isn’t executed yet
feedByNeighbor(); // Jon feeds the dog chimken at 12 noon
The feedByNeighbor function retains the context of neighbor no matter where it is called.
This is especially useful when passing methods as callbacks or event handlers, where this may otherwise get lost.
Example:
const button = document.querySelector("button");
button.addEventListener("click", person.feedDog.bind(person, "kibble", "7 am"));
Without bind(), this would refer to the button element instead of person.
Summary: call() vs apply() vs bind()
All three methods—call(), apply(), and bind()—allow you to manually control the value of this in JavaScript.
Use
call()when you know the exact arguments.Use
apply()when arguments are in an array.Use
bind()when you need to preserve context for later execution.
| Method | Invokes Immediately | Arguments Format | Returns a Function | Common Use Case |
| call() | Yes | Comma-separated | No | Direct invocation with known args |
| apply() | Yes | Array or array-like | No | Invoking with dynamic/array args |
| bind() | No | Comma-separated | Yes | Preserving this for later calls |
Comments
Join the discussionNo comments yet. Be the first to comment.
