1
00:00:00,810 --> 00:00:06,000
Welcome back in the last video we examine the second rule for determining the value of the key word

2
00:00:06,030 --> 00:00:12,060
this the implicit rule the implicit rule states that when the key word this is inside of a declared

3
00:00:12,090 --> 00:00:16,980
object its value will always be the closest parent object.

4
00:00:16,980 --> 00:00:21,780
One thing to note these rules that we are using are not an official part of the JavaScript specification

5
00:00:21,990 --> 00:00:23,710
so you will not find them there.

6
00:00:23,730 --> 00:00:28,910
These rules are just a helpful way of keeping track of what the value of the key word this is.

7
00:00:29,310 --> 00:00:34,740
At the end of the last video we saw an example of when the context of the keyword this changed unexpectedly

8
00:00:35,340 --> 00:00:36,850
in our Say hello method.

9
00:00:36,940 --> 00:00:44,760
The first name property became undefined because the keyword this now referred to the dog object.

10
00:00:44,760 --> 00:00:50,280
So what do we want to explicitly set the value of the keyword this so we can force it to be the person

11
00:00:50,280 --> 00:00:53,210
object instead of the dog object.

12
00:00:53,220 --> 00:00:56,250
This is where the third rule comes in.

13
00:00:56,250 --> 00:01:01,800
Whenever you see the call apply or bind methods you can easily determine what the value of the keyword

14
00:01:01,830 --> 00:01:08,250
this will be because you get to set it as the first parameter to call apply and bind.

15
00:01:08,700 --> 00:01:11,150
Before we compare and contrast these three methods.

16
00:01:11,220 --> 00:01:17,610
One thing to remember is that they can only be used on functions call apply and bind or methods that

17
00:01:17,610 --> 00:01:24,740
can only be used by functions not by any other data type like strings booleans or objects.

18
00:01:24,750 --> 00:01:29,640
The first method we're going to talk about is call the first argument to the call method is whatever

19
00:01:29,640 --> 00:01:32,670
you want the value of the keyword this to be.

20
00:01:32,670 --> 00:01:37,370
This is commonly called this ARG and can be set to whatever you want.

21
00:01:37,530 --> 00:01:43,350
The arguments after this ARG are any parameters that you want to pass to the function which you are

22
00:01:43,350 --> 00:01:49,020
changing the context of the keyword this inside of the sounds confusing but I'll make more sense when

23
00:01:49,020 --> 00:01:50,550
we see an example.

24
00:01:50,550 --> 00:01:55,620
And since a function can have an infinite number of arguments we separate each one of them with a comma

25
00:01:55,680 --> 00:01:57,080
when using call.

26
00:01:57,150 --> 00:02:03,230
Finally when the call method is used on a function that function is immediately invoked.

27
00:02:03,230 --> 00:02:04,600
Now let's move on to a plot.

28
00:02:04,920 --> 00:02:11,940
Apply is almost identical to call except apply only takes two parameters at most the first just like

29
00:02:11,940 --> 00:02:15,630
call is what we want the value of the key word this to be.

30
00:02:15,630 --> 00:02:17,700
We call that this ARG.

31
00:02:17,880 --> 00:02:23,190
The second is an array of arguments that we want to pass to the function in which we are changing the

32
00:02:23,190 --> 00:02:25,600
value of the keyword this.

33
00:02:25,680 --> 00:02:27,060
Now let's examine bind.

34
00:02:27,330 --> 00:02:32,490
Bind is almost identical to call except instead of invoking the function immediately.

35
00:02:32,550 --> 00:02:35,070
Bind returns a function definition.

36
00:02:35,490 --> 00:02:38,410
Let's quickly recap what I mean by function definition.

37
00:02:38,430 --> 00:02:43,270
I'm going to make a function called first function and it is going to return the string hello.

38
00:02:43,650 --> 00:02:47,570
If I do not invoke the function but examine the variable i created.

39
00:02:47,760 --> 00:02:50,480
I'm returned a function definition.

40
00:02:50,490 --> 00:02:55,900
Bind is an extremely powerful function as we can see functions with a different value of the keyword

41
00:02:55,980 --> 00:02:59,710
this and invoke them at a later point in time.

42
00:02:59,850 --> 00:03:05,040
Bind is quite valuable when working with asynchronous code like set timeout and is the building block

43
00:03:05,100 --> 00:03:08,720
for more advanced programming techniques like couriering.

44
00:03:08,790 --> 00:03:15,180
Let's quickly recap these three methods call apply and bind are used to explicitly said the value of

45
00:03:15,180 --> 00:03:15,780
the keyword.

46
00:03:15,810 --> 00:03:21,000
This we use them when we want full control over what the keyword this will refer to.

47
00:03:21,300 --> 00:03:27,480
So it will have precedence over the first two rules call and apply will immediately invoke the function

48
00:03:27,600 --> 00:03:32,940
they are called on whereas bind or return a new function definition with the value of the keyword.

49
00:03:32,940 --> 00:03:36,640
This explicitly set finally call and bind.

50
00:03:36,660 --> 00:03:42,430
Except an infinite number of parameters whereas apply only takes in two in the next video.

51
00:03:42,450 --> 00:03:48,150
We'll see an example of call and how we can use it to explicitly set the value of the keyword this.

52
00:03:48,320 --> 00:03:48,750
See that
