Call Apply Bind In JavaScript
Last Updated : Mar 11, 2024
IN - JavaScript | Written & Updated By - Amruta
In this article we will show you the solution of call apply bind in JavaScript, a powerful programming language using a reputation for working with functions is JavaScript.
Functions can be assigned as variables, supplied as arguments, then returned from by additional functions using JavaScript since they are first-class citizens. Strong approaches for function activation and context modification become possible by this flexibility.
The three fundamental JavaScript methods of call, apply, and bind will be presented in this article.
These techniques provide for more flexible and reusable source code through enabling one to modify the setting of the function and provide parameters to it.
Starting with call and apply, both are used to call functions using particular contexts and arguments, that we will go over every one of the three approaches individually.
After that, we'll explore bind, which generates an additional function with a predetermined context.
Step By Step Guide On Call Apply Bind In JavaScript :-
1. Using ‘call’ method
const person = { fName: function (city, state) { return `${this.first_Name} ${this.last_Name} lives in ${city}, ${state}.`; }, }; const name = { first_Name: 'ABC', last_Name: 'XYZ' }; const result = person.fName.call(name, 'Pune', 'Maharashtra'); console.log(result);
- A single method, fName is used for defining an object called person. City and state are both of the parameters used in this procedure.
- The first_Name and last_Name attributes of the object (using this) are combined with the given city and state arguments inside the fName function to return a string that specifies the location of the individual.
- The first_Name and last_Name areas of an additional object called name are specified. This item indicates a person's name.
- The name object is utilized as a representation of this inside the method to call the fName method of an individual object through the call method. You can directly specify the context (this value) of a function using the call method. In this instance, the name object is set as the current context inside fName.
- Extra arguments given by the fName method are likewise accepted by the call method. In this particular case, the fName method is given the inputs "Pune" and "Maharashtra."
- The variable result contains the outcome of calling the fName method using the given setting and arguments.
2. Using ‘apply’ method
const person = { full_Name: function (city, state) { return `${this.fName} ${this.lName} lives in ${city}, ${state}.`; }, }; const name = { fName: 'ABC', lName: 'XYZ' }; const place = ['Pune', 'Maharashtra']; const result = person.full_Name.apply(name, place); console.log(result);
- A single method, full_Name is used for defining an object called person. City and state are both of the parameters used in this procedure.
- The fName and lName attributes of the object (using this) are combined with the given city and state arguments inside the full_Name function to return a string that specifies the location of the individual.
- The fName and lName areas of an additional object called name are specified. This item indicates a person's name.
- Two items, 'Pune' and 'Maharashtra', which represent the state and city, accordingly, are defined as components of an array named where they are.
- The individual object's full_Name method is called using the apply method. The location (this value) upon which the method will be called, which is the name object, must be applied as the first parameter. The second argument that takes place, is an array that will be utilized as the full_Name method's arguments.
- The variable result contains the outcome of calling the fName method using the given setting and arguments.
3. Using Bind
const person = { full_Name: function (city,state, country) { return `${this.first_Name} ${this.last_Name} lives in ${city}, ${state}, ${country}.`; }, }; const name = { first_Name: 'ABC', last_Name: 'XYZ' }; const boundFunction = person.full_Name.bind(name, 'Pune', 'Maharashtra', 'India'); const result = boundFunction(); console.log(result);
- A single method, full_Name is used for defining an object called person. City, state and country are the parameters used in this procedure.
- The first_Name and last_Name attributes of the object (using this) are combined with the given city, state and country arguments inside the fName function to return a string that specifies the location of the individual.
- The first_Name and last_Name areas of an additional object called name are specified. This item indicates a person's name.
- The person object's full_Name method is attached to a new function called boundFunction using the bind method. The context (this value) and predefined parameters for a function can be set using the bind method. In this particular case, "Pune," "Maharashtra," and "India" have been chosen as arguments and name has been selected as the context.
- To produce the outcome, the boundFunction is called without any arguments. No further arguments are needed considering the arguments were already present throughout the bind process.
- Finally, console.log is used to log the outcome to the console.
Conclusion :-
The call, apply, and bind methods of JavaScript which are crucial instruments for function execution and context manipulation have been covered in this article.
These techniques let you alter the function's context or pass parameters in a dynamic way, increasing the adaptability and reuse of your code.
By understanding these methods, you'll have a potent arsenal of tools at your disposal to take on difficult programming duties and produce more efficient and sustainable JavaScript code.
I hope this article on call apply bind in JavaScript helps you and the steps and method mentioned above are easy to follow and implement.