Welcome back in the previous video we saw some good use cases for call and apply.
We saw how to explicitly set the context of the key word this and pass arguments to the function that
is being altered by call or apply in the case of call arguments are passed as comma separated values
but with Apply arguments are passed as values in an array.
Bind works just like call but instead of calling the function right away it returns a function definition
with a keyword this set to the value of this arc.
So when is bind useful one common use case is when we do not know all of the arguments that will be
passed to a function which means we don't want to invoke the function right away.
We just want to return a new function with some of the parameters set.
We call this partial application.
In the example below we can see that bind is returning a function definition to us.
So we still need an extra set of parentheses to call the function.
What's neat about bind is that we do not need to know all the parameters to the function.
When we bind it we only need to know what we want the value of the keyword this to be.
It's a little bit tricky.
So make sure to pause the video and type the examples on the slide and play around with partial application
.
Another very common use case of BIND is to set the context of the keyword.
This for a function that will be called at a later point in time.
Very commonly This happens when dealing with asynchronous code or code that doesn't run line by line
before you take a look at these examples.
Let's quickly review the asynchronous set timeout method.
So timeout is a method on the window object that's used to execute a function once after a specified
amount of time.
Remember that said timeout is the method on the window object.
That's something we're going to need to remember for a later example.
The first parameter to set timeout is a function to be executed.
And the second parameter is the amount of time in milliseconds to wait before executing the function
.
Let's code a quick example and cancelled out Lague Hello world.
After 20 seconds I'm going to call set timeout and pass in a function that will counseled out loud Hello
world after 20 seconds.
Now that I've called this I can actually do other things like define new variables or continue running
code.
This is what we mean by asynchronous.
Our code is not blocked while we wait 20 seconds and we can do other things while we wait.
There goes our council that blog.
Great.
And the next video will come back to talking about the keyword this.
And take a look at a pretty tricky example.
CNN