How-To: Call, Apply and Bind in Javascript: Niladri Sekhar Dutta
How-To: Call, Apply and Bind in Javascript: Niladri Sekhar Dutta
How-To: Call, Apply and Bind in Javascript: Niladri Sekhar Dutta
Uses
You 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.
or Function.prototype.call()
Enjoy this post?
call() WRITE A POST
34
https://www.codementor.io/niladrisekhardutta/how-to-call-apply-and-bind-in-javascript-8i1jca6jp 1/7
1/17/2019 How-to: call() , apply() and bind() in JavaScript | Codementor
console.log(greeting.call(obj,"Newtown","KOLKATA","WB"));
// returns output as welcome Niladri to Newtown KOLKATA in WB
The rst parameter in call() method sets the "this" value, which is the
object, on which the function is invoked upon. 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()
Similarly to call() method the rst 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 di erence 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()
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 .
https://www.codementor.io/niladrisekhardutta/how-to-call-apply-and-bind-in-javascript-8i1jca6jp 4/7
1/17/2019 How-to: call() , apply() and bind() in JavaScript | Codementor
The rst 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 rst
parameter is ignored if the bound function is constructed using the "new"
operator.
The rest of the parameters following the rst 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.
Enjoy this post? Give Niladri Sekhar Dutta a like if it's helpful.
34 18 SHARE
Enjoy this post? WRITE A POST
34
https://www.codementor.io/niladrisekhardutta/how-to-call-apply-and-bind-in-javascript-8i1jca6jp 5/7
1/17/2019 How-to: call() , apply() and bind() in JavaScript | Codementor
FOLLOW
18 Replies
Leave a reply
Reply
clear explanation ♥
1 Reply
https://www.codementor.io/niladrisekhardutta/how-to-call-apply-and-bind-in-javascript-8i1jca6jp 6/7
1/17/2019 How-to: call() , apply() and bind() in JavaScript | Codementor
2 Reply
Dinesh Pandiyan
Tanner Linsley
Sohaib Nehal
https://www.codementor.io/niladrisekhardutta/how-to-call-apply-and-bind-in-javascript-8i1jca6jp 7/7