How To Call(), Apply() And Bind() In JavaScript

 
 
In this post, we will be discussing the difference between call(), apply(), and bind() methods of JavaScript functions with simple examples. As functions are also Objects in JavaScript, these 3 methods are used to control the invocation of the function. call() and apply() were introduced in ECMAScript 3 while bind() was added as part of ECMAScript 5.

Uses

We can use call()/apply() to invoke the function immediately. bind() returns a bound function that, when executed later, will have the correct context ("this") for calling the original function. So bind() can be used when the function needs to be called later in certain events when it's useful.

To get a grasp of "this" in JavaScript, read Understanding "this" in javascript and also Difference between scope and context in javascript.

call() or Function.prototype.call()

Check the code sample below for call(),

  1. //Demo with javascript .call()  
  2.   
  3. var obj = {name:"Niladri"};  
  4.   
  5. var greeting = function(a,b,c){  
  6.     return "welcome "+this.name+" to "+a+" "+b+" in "+c;  
  7. };  
  8.   
  9. console.log(greeting.call(obj,"Newtown","KOLKATA","WB"));  
  10. // returns output as welcome Niladri to Newtown KOLKATA in WB 
The first parameter in call() method sets the "this" value, which is the object, on which the function is invoked. In this case, it's the "obj" object above.

The rest of the parameters are the arguments to the actual function. 

apply() or Function.prototype.apply()

Check the below code sample for apply()
  1. //Demo with javascript .apply()  
  2.   
  3. var obj = {name:"Niladri"};  
  4.   
  5. var greeting = function(a,b,c){  
  6.     return "welcome "+this.name+" to "+a+" "+b+" in "+c;  
  7. };  
  8.   
  9. // array of arguments to the actual function  
  10. var args = ["Newtown","KOLKATA","WB"];    
  11. console.log("Output using .apply() below ")  
  12. console.log(greeting.apply(obj,args));  
  13.   
  14. /* The output will be  
  15.   Output using .apply() below 
  16.  welcome Niladri to Newtown KOLKATA in WB */ 
Similarly to call() method the first parameter in apply() method sets the "this" value which is the object upon which the function is invoked. In this case it's the "obj" object above. The only difference of apply() with the call() method is that the second parameter of the apply() method accepts the arguments to the actual function as an array.

bind() or Function.prototype.bind()

Check the below code sample for bind()
  1. //Use .bind() javascript  
  2.   
  3. var obj = {name:"Niladri"};  
  4.   
  5. var greeting = function(a,b,c){  
  6.     return "welcome "+this.name+" to "+a+" "+b+" in "+c;  
  7. };  
  8.   
  9. //creates a bound function that has same body and parameters   
  10. var bound = greeting.bind(obj);   
  11.   
  12.   
  13. console.dir(bound); ///returns a function  
  14.   
  15. console.log("Output using .bind() below ");  
  16.   
  17. console.log(bound("Newtown","KOLKATA","WB")); //call the bound function  
  18.   
  19. /* the output will be  
  20. Output using .bind() below 
  21. welcome Niladri to Newtown KOLKATA in WB */ 
In the above code sample for bind() we are returning a bound function with the context which will be invoked later. We can see the bound function in the console as below .
 


The first parameter to the bind() method sets the value of "this" in the target function when the bound function is called. Please note that the value for first parameter is ignored if the bound function is constructed using the "new" operator.

The rest of the parameters following the first parameter in bind() method are passed as arguments which are prepended to the arguments provided to the bound function when invoking the target function.

That's all for now. Thank you for reading and I hope this post will be helpful for beginners who are facing issues regarding the apply(), call(), and bind() methods of JavaScript.