Explain call() and apply() methods in JavaScript
Last Updated :
28 Mar, 2022
Call() Method: The call method is basically used to invoke the function with different this object. In JavaScript, this refers to an object. It depends on how we are calling a particular function. In the global scope, this refers to the global object window. Inside function also this refers to the global object window.
In strict mode, when we use any function then this refers to undefined. In functions like call, this could refer to a different object. With the help of the call method, we can invoke a particular function with different objects.
Syntax:
object.objectMethod.call( objectInstance, arguments )
Parameters: It takes two parameters:
- ObjectInstance: It is an object which we want to use explicitly
- Arguments: It is arguments that we want to pass to the calling function
Example 1:
JavaScript
<script>
const obj = {
firstName: "First_name",
lastName: "Last_name",
printName: function () {
console.log(this.firstName
+ " " + this.lastName);
}
};
obj.printName();
</script>
Output:
First_name Last_name
It's obvious since we are calling the printName( ) function with the object obj so this should refer to object obj. And inside object obj we have firstName = "First_name" and lastName = "Last_name" so output is "First_name Last_name".
Example 2: As we know, we can make invoke a particular function with different objects. So we can invoke a function the printName with different objects also.
JavaScript
<script>
const obj1 = {
firstName: "First_name",
lastName: "Last_name"
};
const obj2 = {
firstName: "Sachin",
lastName: "Tendulkar"
};
function printName() {
console.log(this.firstName + " " + this.lastName);
}
printName.call(obj2);
</script>
Output:
Sachin Tendulkar
In this case, we are using the call method to invoke the function with the object obj2. So in this case this would be referring to object obj2. Because this depends upon how we are actually invoking the function. In this case, this will not refer to the global object window but refer to object obj2.
Passing parameter with the call method: We can also pass the parameter to the call function.
Example 3:
JavaScript
<script>
const obj1 = {
firstName: "First_name",
lastName: "Last_name"
};
const obj2 = {
firstName: "Sachin",
lastName: "Tendulkar"
};
function printName(profession, country) {
console.log(this.firstName + " "
+ this.lastName + " " +
profession + " " + country);
}
printName.call(obj2, "Cricketer", "India");
</script>
Output:
Sachin Tendulkar Cricketer India
When we pass parameters using the call method then we pass parameters separated by commas( , ).
Apply() method: Just like the call method we can also bind the function to any object. Using apply( ) method also we can invoke a given function with different objects.
Syntax:
object.objectMethod.apply(objectInstance, arrayOfArguments)
Parameters: It takes two parameters:
- ObjectInstance: It is an object which we want to use explicitly
- Arguments: It is arguments that we want to pass to the calling function
JavaScript
<script>
const obj1 = {
firstName: "First_name",
lastName: "Last_name"
};
const obj2 = {
firstName: "Sachin",
lastName: "Tendulkar"
};
function printName() {
console.log(this.firstName + " " + this.lastName);
}
printName.apply(obj2);
</script>
Output:
Sachin Tendulkar
Passing parameter with the apply method: We can also pass parameters with apply function.
JavaScript
<script>
const obj1 = {
firstName: "First_name",
lastName: "Last_name"
};
const obj2 = {
firstName: "Sachin",
lastName: "Tendulkar"
};
function printName(profession, country) {
console.log(this.firstName + " "
+ this.lastName + " " +
profession + " " + country);
}
printName.apply(obj2, ["Cricketer", "India"]);
</script>
Output:
Sachin Tendulkar Cricketer India
Similar Reads
Explain call(), apply(), and bind() methods in JavaScript
JavaScript provides the call(), apply(), and bind() methods for setting the this context within a function. These methods are especially useful when working with object-oriented code or handling different function contexts. In this article, weâll explore what these methods do, provide examples, and
7 min read
JavaScript Handler apply() Method
JavaScript handler.apply() method in JavaScript is used as a trap for a function call. The value returned by this method is used as the result of a function call through a proxy. Syntax: const p = new Proxy(target, { apply: function(target, thisArg, argumentsList) { } }); Parameters: This method acc
2 min read
Explain Scope and Scope Chain in JavaScript
In this article, we will try to understand what is the scope of a variable as well as its function (or method). We will see what is a Scope Chain with the help of certain coding examples. ScopeScope in JavaScript determines the accessibility of variables and functions at various parts of one's code
5 min read
JavaScript Array Iteration Methods
JavaScript Array iteration methods perform some operation on each element of an array. Array iteration means accessing each element of an array. There are some examples of Array iteration methods are given below: Using Array forEach() MethodUsing Array some() MethodUsing Array map() MethodMethod 1:
3 min read
What is the difference between call and apply in JavaScript ?
JavaScript call() Method: It calls the method, taking the owner object as an argument. The keyword this refers to the 'owner' of the function or the object it belongs to. We can call a method that can be used on different objects. Syntax: object.objectMethod.call( objectInstance, arguments ) JavaScr
2 min read
How to clone an array in JavaScript ?
In JavaScript, cloning an array means creating a new array with the same elements as the original array without modifying the original array.Here are some common use cases for cloning an array:Table of ContentUsing the Array.slice() MethodUsing the spread OperatorUsing the Array.from() MethodUsing t
6 min read
JavaScript Handler set() Method
JavaScript handler.set() method in JavaScript is a trap for setting a property value. This method returns a boolean value. Syntax: const p = new Proxy(target, { set: function(target, property, value, receiver) { } }); Parameters: This method accepts four parameters as mentioned above and described b
2 min read
Classes and Objects in JavaScript
Classes Classes were first introduced in the new version of the ES6 classes which replaced the previously used functions. Class is nothing but a blueprint for an object of it. It is used to create an object mainly. If we relate it to a real-life example then it is like a plan for a building or house
4 min read
Applications of Map in JavaScript
Javascript Map is a collection of elements where each element is stored as a Key, value pair. Map objects can hold both objects and primitive values as either key or value. When we iterate over the map object it returns the key, and value pair in the same order as inserted. Syntax: new Map([it]); Pa
5 min read
Important Array Methods of JavaScript
JavaScript arrays are powerful tools for managing collections of data. They come with a wide range of built-in methods that allow developers to manipulate, transform, and interact with array elements.Some of the most important array methods in JavaScript areTable of Content1. JavaScript push() Metho
7 min read