by Rajat Saxena
Let’s drill it into our minds and be done with it, once and for all.
I’m writing this micro post because the aforementioned question has haunted me for a very long time, and I knew I wasn’t the only one. Every single time I saw someone use any of those three methods I had to rush to MDN in order to figure out what was going on.
Disclaimer: This is not a theoretical solution, but a really hacky way to remember the difference.
The major cause of confusion between the
apply() methods is how to pass in the additional arguments besides
this. And why do we have
So let’s learn how to easily tell the three apart.
apply(this [, [arg1, arg2,...]]): Calls a function with a provided
this value. Further arguments are provided as a single array.
Way to remember: “Apply accepts arguments as an Array” or “AA”
call(this [, arg1, arg2...]): Calls a function with a provided
this. Further arguments are provided as a comma separated list
Ways to remember: “Call’s arguments are separated by commas” or “CC”.
bind(this): Returns a new function whose
this value is bound to the provided value.
Ways to remember: bind() is the only method out of the three that returns a new function altogether. It does not call the function.
I hope the above explanation might help some of you out there. It certainly is helping me.
Do you have other memorization tricks related to programming? Kindly share it with the community as it will help everyone out. Especially during those interviews.