1
00:00:00,450 --> 00:00:05,690
Welcome back in the previous video we discussed the third rule for determining the value of the key

2
00:00:05,690 --> 00:00:08,430
word this explicit binding.

3
00:00:08,430 --> 00:00:13,470
We learned that using call apply and bind can change the context of the key word.

4
00:00:13,480 --> 00:00:19,890
This Let's see how we can fix our previous issue by using call the first code block is identical to

5
00:00:19,890 --> 00:00:20,990
what we saw before.

6
00:00:21,180 --> 00:00:24,090
But let's see what's happening in the second section.

7
00:00:24,150 --> 00:00:29,490
Previously we were calling person dog dog say hello which was returning Hello.

8
00:00:29,520 --> 00:00:36,270
Undefined does happen because the value of the keyword this inside of the say hello function refers

9
00:00:36,270 --> 00:00:40,620
to the dog object which does not have a first name property.

10
00:00:40,620 --> 00:00:45,660
Since we're using call we can now change what we want the value of the keyword this to be.

11
00:00:45,870 --> 00:00:48,840
So let's change it to be the person object.

12
00:00:49,140 --> 00:00:55,590
Now that we've done that we can see that the function returns Hello called and our issue is fixed.

13
00:00:55,590 --> 00:01:01,400
We can also use the determine context function inside the dog object and change the value of the keyword

14
00:01:01,440 --> 00:01:04,350
this to make the function return true.

15
00:01:04,800 --> 00:01:10,800
One important thing to note is that we are not invoking the say hello or determine context method we

16
00:01:10,800 --> 00:01:12,650
just attach call onto it.

17
00:01:12,690 --> 00:01:16,750
So there are no parentheses after say hello and determine context.

18
00:01:16,830 --> 00:01:25,030
When we use call very commonly call is used to avoid code application like this example below.

19
00:01:25,110 --> 00:01:30,780
Here we have two objects cold and L.E. which both have a first name property and they say Hi method

20
00:01:30,780 --> 00:01:31,920
.

21
00:01:31,920 --> 00:01:36,750
While this code works totally fine we're repeating ourselves quite a bit inside of the elete object

22
00:01:37,140 --> 00:01:44,610
to say hi method inside of the object is identical to the same method inside of the cold object.

23
00:01:44,610 --> 00:01:50,340
How can we refactor our code and get rid of the high method inside of the object.

24
00:01:50,340 --> 00:01:54,750
What we really like to do is use the same high method from the cult object.

25
00:01:54,750 --> 00:02:00,870
But instead of the keyword this referring to cold we want to explicitly set it to be the L-E object

26
00:02:00,870 --> 00:02:01,530
.

27
00:02:01,530 --> 00:02:06,690
Pause the video now and see if you can figure out how to solve the problem using call.

28
00:02:06,690 --> 00:02:08,470
Let's take a look at a solution.

29
00:02:08,490 --> 00:02:15,120
We remove the say hi method from the object and we are using call to set the value of the keyword this

30
00:02:15,270 --> 00:02:16,320
to be L-E.

31
00:02:16,380 --> 00:02:22,260
When the say hi method is being invoked the first parameter to the call method is what we want the keyword

32
00:02:22,320 --> 00:02:24,350
this to refer to.

33
00:02:24,540 --> 00:02:26,250
We call that this ARG.

34
00:02:26,940 --> 00:02:28,600
Once again this stuff is quite tricky.

35
00:02:28,620 --> 00:02:33,330
So if you're feeling a bit confused look at the previous slide and make sure you type these examples

36
00:02:33,390 --> 00:02:38,540
into the chrome console to see exactly what's going on in the next video.

37
00:02:38,610 --> 00:02:43,890
We're going to take a look at apply and see how we can set the context of the keyword this using apply

38
00:02:44,190 --> 00:02:47,700
as well as passing arguments to the function in which we call apply.

39
00:02:47,700 --> 00:02:48,850
On the other
