Call(), Apply(),Bind() Methods Of JavaScript

Vibhor Bhatia
3 min readMar 25, 2020

Before understanding the powerhouse functions of JS, we need to understand the purpose of this keyword.
Sometimes “this” uses as window object and sometimes it uses as an invoking a function, it uses varies from situation to situation.

When there is no object in a function and its getting invoked then “this” will be taking the window object value but if a function getting invoked by an object then “this” refers to the object which invokes the function. Let’s see below example to understand it better-

Function CounTer() is getting invoke by fruits and “this” inside CounTer refers to fruits.
Function Counter2() is getting invoke inside function Counter() and no object is invoking it so “this” will be referring window object ( apple = 10)

Now have a look into the functionality of powerhouse functions of JavaScipt-

The above snippet will give an output-
‘’I love Apple and Mango’’

Call() method will be used as a borrowing function which will make your life easier when you need to set the value of “this”. Let’s see below snippet-

fruits.printFruits.call() will borrow the function and used with different object.
Here fruits2 will be pass as argument which will borrow the function property of printFruits() from fruits object.
The output of above Snippet will be-

‘’I love Apple and Mango’’

I love Watermelon and Muskmelon

We can pass multiple parameters to call() method but the first parameter should be a reference of our “this” and rest we can pass the parameter on our requirements. See below example-

Here, we are passing first parameter as Object name , City and State as the second and third parameters. We can multiple parameters which should be separated by “ ,

Apply() and Call() serve the exact same purpose. The only difference between them is call() expects all the parameters to be passed in individually, whereas in apply() it expects an array of our parameter passed as a second parameter.

Bind() method looks similar to call() method but it will bind the function to the object and provide a copy of it.
Advantage of using bind() method, we can invoke this method later into our code.

Let’s understand it through example-

If you have any question, you can reach me on LinkedIn

--

--