Apply, Call, Bind, & arguments
Learn 'apply', 'call', and 'bind', three functions that give us control over the 'this' value inside of a function. We'll see how these methods allow us to write functions that can accept any number of arguments.
We'll cover the following
We are going to showcase a way to explicitly set the value of this
in a function. We can use the apply
, call
, and bind
methods. These are simple methods, but crucial to understanding how to read code and write proper object oriented JavaScript.
Occasionally, it’s more useful and makes more sense for the this
value to be some other object than what JavaScript wants to set it to. We can override some of the automatic cases of this
inside functions by using these three methods.
Occasionally, we want to use a method of one object on another. Remember that in OOP, methods are meant to act on the object that they are a property of. It’s often useful to take these methods and apply them (no pun intended) to different objects to perform the same action.
This prevents code duplication and makes it clear that we are using a method on a different object than it was intended.
call
Get hands-on with 1400+ tech skills courses.