1
00:00:00,480 --> 00:00:05,570
Welcome back in the previous video we saw some good use cases for call and apply.

2
00:00:05,700 --> 00:00:11,160
We saw how to explicitly set the context of the key word this and pass arguments to the function that

3
00:00:11,160 --> 00:00:17,440
is being altered by call or apply in the case of call arguments are passed as comma separated values

4
00:00:17,670 --> 00:00:21,570
but with Apply arguments are passed as values in an array.

5
00:00:21,960 --> 00:00:28,590
Bind works just like call but instead of calling the function right away it returns a function definition

6
00:00:28,890 --> 00:00:33,730
with a keyword this set to the value of this arc.

7
00:00:33,750 --> 00:00:39,030
So when is bind useful one common use case is when we do not know all of the arguments that will be

8
00:00:39,030 --> 00:00:43,440
passed to a function which means we don't want to invoke the function right away.

9
00:00:43,530 --> 00:00:47,310
We just want to return a new function with some of the parameters set.

10
00:00:47,310 --> 00:00:49,510
We call this partial application.

11
00:00:50,010 --> 00:00:54,780
In the example below we can see that bind is returning a function definition to us.

12
00:00:54,780 --> 00:00:58,740
So we still need an extra set of parentheses to call the function.

13
00:00:58,740 --> 00:01:03,680
What's neat about bind is that we do not need to know all the parameters to the function.

14
00:01:03,690 --> 00:01:09,880
When we bind it we only need to know what we want the value of the keyword this to be.

15
00:01:09,900 --> 00:01:11,050
It's a little bit tricky.

16
00:01:11,070 --> 00:01:16,830
So make sure to pause the video and type the examples on the slide and play around with partial application

17
00:01:16,830 --> 00:01:18,150
.

18
00:01:18,150 --> 00:01:22,280
Another very common use case of BIND is to set the context of the keyword.

19
00:01:22,280 --> 00:01:26,200
This for a function that will be called at a later point in time.

20
00:01:26,460 --> 00:01:32,100
Very commonly This happens when dealing with asynchronous code or code that doesn't run line by line

21
00:01:32,730 --> 00:01:34,850
before you take a look at these examples.

22
00:01:34,860 --> 00:01:38,230
Let's quickly review the asynchronous set timeout method.

23
00:01:38,490 --> 00:01:44,310
So timeout is a method on the window object that's used to execute a function once after a specified

24
00:01:44,340 --> 00:01:45,690
amount of time.

25
00:01:45,810 --> 00:01:49,060
Remember that said timeout is the method on the window object.

26
00:01:49,110 --> 00:01:52,750
That's something we're going to need to remember for a later example.

27
00:01:52,830 --> 00:01:56,460
The first parameter to set timeout is a function to be executed.

28
00:01:56,550 --> 00:02:01,230
And the second parameter is the amount of time in milliseconds to wait before executing the function

29
00:02:01,240 --> 00:02:01,800
.

30
00:02:02,340 --> 00:02:05,380
Let's code a quick example and cancelled out Lague Hello world.

31
00:02:05,400 --> 00:02:11,070
After 20 seconds I'm going to call set timeout and pass in a function that will counseled out loud Hello

32
00:02:11,070 --> 00:02:13,340
world after 20 seconds.

33
00:02:13,350 --> 00:02:18,450
Now that I've called this I can actually do other things like define new variables or continue running

34
00:02:18,450 --> 00:02:19,160
code.

35
00:02:19,170 --> 00:02:21,350
This is what we mean by asynchronous.

36
00:02:21,420 --> 00:02:25,820
Our code is not blocked while we wait 20 seconds and we can do other things while we wait.

37
00:02:25,830 --> 00:02:27,060
There goes our council that blog.

38
00:02:27,130 --> 00:02:29,380
Great.

39
00:02:30,000 --> 00:02:33,180
And the next video will come back to talking about the keyword this.

40
00:02:33,180 --> 00:02:36,000
And take a look at a pretty tricky example.

41
00:02:36,100 --> 00:02:36,330
CNN
