Now let's see what happens when we bring the key word this back into the mix.
Here's where things get fun in the first code block.
We have a method called Say hi which is a function that calls a set timeout and cancelled out logs the
string high concatenated with the value of the first name property on the keyword.
This before we see what this code returns.
Pause the video and think about what the key word this refers to.
Go back to the first two rules is the key word this in the global context.
Or is the key word this inside of a declared object.
You might be thinking it's clearly inside of a declared object and you are right.
However since the set timeout is called at a later point in time the keyword this does not refer to
the parent object.
It actually refers to the global object.
This is very tricky and trips up lots of beginners.
So let's walk through it again since the set timeout is called at a later point in time.
The object that it is attached to is actually the window just like we said before set time out is a
method on the window object even though it's defined inside of the cold object when it's declared the
context in which the function is executed is actually the global context.
So how can we solve this problem since we're losing the correct context of the key word.
This we should explicitly set but we want the key word this to refer to.
We can do this using call and apply but since we want to call the function at a later point in time
we don't want to use either of those.
Remember that call and apply invoke the function right away.
So doing that would defeat the purpose of a set time out which is for executing a function at a later
point in time.
So that leaves us with bind in the second code block we can solve our problem by passing in as the first
parameter to the bind method.
The value that we want the keyword this to refer to you may be totally confused as to why we pass in
the keyword this as the first parameter.
But take a step back and ask yourself inside of the code object what does the keyword this refer to
.
Well it actually refers to the cult object itself.
So we are binding the correct value of the keyword this to be what we want when the function inside
of set time out is called instead of the keyword this you can pass in the variable called to the bind
method and you will get the same result.
But more commonly you will see code that uses the keyword this.
To recap we saw that the bind method returns a function definition unlike call an apply and is very
useful for setting the value of the keyword this when we do not know all of the values for arguments
to pass to the function or when we are working with asynchronous code.
One final note.
The material we're covering here is quite advanced.
But if you're looking to become a professional javascript developer this is the kind of code you'll
see in production code bases as well as get asked about in interviews if you're still quite tripped
up with this example.
Don't worry.
I've taught this topic a lot and it's always something that confuses people at first try to walk through
each line of code and make sure you type the examples in the chrome console to see what's going on.
As always feel free to ask those questions.
We're here to help you learn in the next video.
We're going to briefly cover the last rule for determining the key word this so either