What is call(), bind() and apply() in JavaScript?

Let's learn about the call() bind() and apply() in details in JavaScript.

  Shubham Verma      July 27, 2021


What is call(), bind() and apply() in JavaScript?

What is call(), bind() and apply()?

As we know in the JS, everything is an object. So we can accessa and add properties in the objects and functions. We also know that every function gets this property automatically. this is used inside a function, and will always refer to a single object. So we use call(), bind() and apply() to get the this of another function and use their properties with our functions. Basically we borrow the method using this for some point of time.

Not everythig is an object in JS, we can say almost everything. Later I'll show you how not everything is an object. So be with this tutorial.

call():

The call() method calls a function with a given this value and arguments provided individually. The syntax of the call() is almost identical to that of 'apply()'. The fundamental difference is that 'call()' accepts an argument list while 'apply()' accepts a 'single array of arguments'.
Syntax:

function.call(thisArg, arg1, arg2, ....., argn);


Return Value: Specified 'this' value and arguments.
Examples:

Or:

apply():

The apply() method also borrows the function and sets the 'this' value in the function invocation. In addition, the apply() in particular allow us to execute a function with an array of paremeters, such that each parameter is passed to the function individually when the function executes.
The apply() calls a function with a given 'this' value and 'arguments' provided as an array.

The syntax of the function is almost identical to the call(), the fundamental difference is that the call() accepts the argument as comma seperated but in the apply(), this accepts an array of arguments.


Syntax:

function.apply(thisArg, [arg1, arg2, ....., argn]);


Return Value: Specified 'this' value and arguments.
Examples:

Or:

Or:

bind():

bind() is a bit different from call() and apply(). It returns a new function associated with binded 'this' while call() and apply() execute the current function Immediately. bind() is great for lot of things:
* We can use bind() for curry functions.
* We can eaisily reuse and we can call whenever it required.
* We can store the reference of the executable functions and later can we call.
* We can also use bind() for events like onClick() where we don't know when they will be fired but know what context we want them and how.
* We can change the context as per the requirements.

Examples:



Conclusion:

In this article, we learned about What is call(), bind() and apply()?, We have implemented all these concepts very well. Now we can use call(), bind() and apply() in our projects.


Support our IDKBlogs team


Welcome to IDKBlogs.

If our contents help you and you learned from this platform, please consider supporting what we do.

Thank you.




Strongly Recommended Books For You:

Reading books opens the door to allow in more lights, If we want to know a man of rare intellect, try to know what books he/she reads.

For India:

For Other Countries (United States, United Kingdom, Spain, Italy, Germany, France, Canada, etc)


Thank you

I appreciate you taking the time to read this article. The more that you read, the more things you will know. The more that you learn, the more places you'll go. If you’re interested in Node.js or JavaScript this link will help you a lot.

If you found this article is helpful, then please share this article's link to your friends to whom this is required, you can share this to your technical social media groups also. You can follow us on our social media page for more updates and latest article updates.
To read more about the technologies, Please subscribe us, You'll get the monthly newsletter having all the published article of the last month.



Funny Programming | Idkblogs.com | Shubham Verma

👈

Enjoyed this moment?