Welcome back in the previous video we discussed the third rule for determining the value of the key
word this explicit binding.
We learned that using call apply and bind can change the context of the key word.
This Let's see how we can fix our previous issue by using call the first code block is identical to
what we saw before.
But let's see what's happening in the second section.
Previously we were calling person dog dog say hello which was returning Hello.
Undefined does happen because the value of the keyword this inside of the say hello function refers
to the dog object which does not have a first name property.
Since we're using call we can now change what we want the value of the keyword this to be.
So let's change it to be the person object.
Now that we've done that we can see that the function returns Hello called and our issue is fixed.
We can also use the determine context function inside the dog object and change the value of the keyword
this to make the function return true.
One important thing to note is that we are not invoking the say hello or determine context method we
just attach call onto it.
So there are no parentheses after say hello and determine context.
When we use call very commonly call is used to avoid code application like this example below.
Here we have two objects cold and L.E. which both have a first name property and they say Hi method
.
While this code works totally fine we're repeating ourselves quite a bit inside of the elete object
to say hi method inside of the object is identical to the same method inside of the cold object.
How can we refactor our code and get rid of the high method inside of the object.
What we really like to do is use the same high method from the cult object.
But instead of the keyword this referring to cold we want to explicitly set it to be the L-E object
.
Pause the video now and see if you can figure out how to solve the problem using call.
Let's take a look at a solution.
We remove the say hi method from the object and we are using call to set the value of the keyword this
to be L-E.
When the say hi method is being invoked the first parameter to the call method is what we want the keyword
this to refer to.
We call that this ARG.
Once again this stuff is quite tricky.
So if you're feeling a bit confused look at the previous slide and make sure you type these examples
into the chrome console to see exactly what's going on in the next video.
We're going to take a look at apply and see how we can set the context of the keyword this using apply
as well as passing arguments to the function in which we call apply.
On the other