Welcome back in the last video we examine the second rule for determining the value of the key word
this the implicit rule the implicit rule states that when the key word this is inside of a declared
object its value will always be the closest parent object.
One thing to note these rules that we are using are not an official part of the JavaScript specification
so you will not find them there.
These rules are just a helpful way of keeping track of what the value of the key word this is.
At the end of the last video we saw an example of when the context of the keyword this changed unexpectedly
in our Say hello method.
The first name property became undefined because the keyword this now referred to the dog object.
So what do we want to explicitly set the value of the keyword this so we can force it to be the person
object instead of the dog object.
This is where the third rule comes in.
Whenever you see the call apply or bind methods you can easily determine what the value of the keyword
this will be because you get to set it as the first parameter to call apply and bind.
Before we compare and contrast these three methods.
One thing to remember is that they can only be used on functions call apply and bind or methods that
can only be used by functions not by any other data type like strings booleans or objects.
The first method we're going to talk about is call the first argument to the call method is whatever
you want the value of the keyword this to be.
This is commonly called this ARG and can be set to whatever you want.
The arguments after this ARG are any parameters that you want to pass to the function which you are
changing the context of the keyword this inside of the sounds confusing but I'll make more sense when
we see an example.
And since a function can have an infinite number of arguments we separate each one of them with a comma
when using call.
Finally when the call method is used on a function that function is immediately invoked.
Now let's move on to a plot.
Apply is almost identical to call except apply only takes two parameters at most the first just like
call is what we want the value of the key word this to be.
We call that this ARG.
The second is an array of arguments that we want to pass to the function in which we are changing the
value of the keyword this.
Now let's examine bind.
Bind is almost identical to call except instead of invoking the function immediately.
Bind returns a function definition.
Let's quickly recap what I mean by function definition.
I'm going to make a function called first function and it is going to return the string hello.
If I do not invoke the function but examine the variable i created.
I'm returned a function definition.
Bind is an extremely powerful function as we can see functions with a different value of the keyword
this and invoke them at a later point in time.
Bind is quite valuable when working with asynchronous code like set timeout and is the building block
for more advanced programming techniques like couriering.
Let's quickly recap these three methods call apply and bind are used to explicitly said the value of
the keyword.
This we use them when we want full control over what the keyword this will refer to.
So it will have precedence over the first two rules call and apply will immediately invoke the function
they are called on whereas bind or return a new function definition with the value of the keyword.
This explicitly set finally call and bind.
Except an infinite number of parameters whereas apply only takes in two in the next video.
We'll see an example of call and how we can use it to explicitly set the value of the keyword this.
See that