1
00:00:02,520 --> 00:00:05,930
So bind is really useful for also managing this.

2
00:00:05,940 --> 00:00:10,940
Now you might argue though that this syntax is a bit redundant or this code here is a bit redundant, if

3
00:00:10,980 --> 00:00:17,430
we pull out get formatted title just to then reassign it to itself with the right this configuration

4
00:00:18,120 --> 00:00:19,140
and you'd be right.

5
00:00:19,140 --> 00:00:24,390
Bind is useful whenever you want to preconfigure a function for the future execution

6
00:00:24,390 --> 00:00:28,190
but here we actually plan on executing the function right away,

7
00:00:28,260 --> 00:00:33,540
so instead of doing that we can also use a different method which we can call on a function. I already

8
00:00:33,540 --> 00:00:38,850
mentioned it briefly earlier in the course but there I said that we would dive into it later,

9
00:00:38,850 --> 00:00:42,120
well now is later. Besides bind,

10
00:00:42,120 --> 00:00:47,630
you also have call. Call also takes multiple arguments,

11
00:00:47,680 --> 00:00:51,180
the first argument just as for bind is what this,

12
00:00:51,250 --> 00:00:56,800
the this keyword should refer to inside of the function that is about to be executed,

13
00:00:56,800 --> 00:01:02,800
second and more arguments, you can add as many as you want, are then the arguments that are passed into

14
00:01:02,800 --> 00:01:05,030
the function if it needs any.

15
00:01:05,090 --> 00:01:09,640
Now here we don't need to pass in any arguments so I'm only interested in the first parameter which

16
00:01:09,640 --> 00:01:12,810
we pass to call and that again would be movie here.

17
00:01:12,850 --> 00:01:19,690
So how is call different from bind then? Well bind prepares a function for future execution, bind

18
00:01:19,690 --> 00:01:25,540
returns a new function object in the end which we then store here in get formatted title, call does

19
00:01:25,540 --> 00:01:30,790
not do that, call instead goes ahead and executes the function right away.

20
00:01:30,790 --> 00:01:38,590
So it's like if you added parentheses here but with that extra twist of allowing you to overwrite what

21
00:01:38,590 --> 00:01:41,230
this inside of the function refers to,

22
00:01:41,380 --> 00:01:45,750
so it executes a function for you when you want to change what this refers to,

23
00:01:45,750 --> 00:01:51,780
that's where call is important and therefore now if I reload, we'll also see that this works again,

24
00:01:51,790 --> 00:01:59,290
if I output Javascript level 10 to mix things up really hard here and we see that works.

25
00:01:59,470 --> 00:02:06,460
So that's call and we also got apply, apply is pretty similar to call, it also will execute the function

26
00:02:06,460 --> 00:02:07,120
right away,

27
00:02:07,120 --> 00:02:12,120
the difference is that there, first argument still is what this should refer to

28
00:02:12,250 --> 00:02:17,800
but then you don't have an infinite amount of additional arguments but instead it only takes one additional

29
00:02:17,800 --> 00:02:24,210
argument and that however has to be an array and that can now be your values for the different arguments

30
00:02:24,220 --> 00:02:30,610
this function might be taking. So in the end, the difference is call allows you to pass additional arguments

31
00:02:30,730 --> 00:02:32,570
as a comma separated list,

32
00:02:32,710 --> 00:02:40,000
apply allows you to pass additional arguments as an array, up to you which one you use,

33
00:02:40,000 --> 00:02:44,830
here we don't pass in any arguments so it makes absolutely no difference and I'll therefore go with

34
00:02:44,830 --> 00:02:50,430
call but apply would work just as well as I can actually prove by using that instead

35
00:02:50,530 --> 00:02:56,520
if we now use Javascript level 11, it works.

36
00:02:56,740 --> 00:03:02,770
So that's call and apply and bind which help us manage this, which can be hard to wrap your head around

37
00:03:02,800 --> 00:03:10,300
but always keep in mind, this inside of a function always refers to what called that function or to

38
00:03:10,300 --> 00:03:10,950
be precise,

39
00:03:11,170 --> 00:03:17,500
always refers to the thing in front of your function execution here you could say. So here we have nothing

40
00:03:17,500 --> 00:03:18,280
in front of it,

41
00:03:18,340 --> 00:03:20,380
hence it's the global context.
