1
00:00:02,651 --> 00:00:04,428
The output of all the three functions is

2
00:00:04,428 --> 00:00:07,651
printed to the console. With call and

3
00:00:07,651 --> 00:00:09,851
apply, we call an existing function and

4
00:00:09,851 --> 00:00:11,776
change the function context, that is, the

5
00:00:11,776 --> 00:00:14,574
value of the this object. But what if we

6
00:00:14,574 --> 00:00:16,651
would like to make a copy of a function,

7
00:00:16,651 --> 00:00:19,095
and then change the value of this? The

8
00:00:19,095 --> 00:00:22,651
bind method allows us to do just that.

9
00:00:22,651 --> 00:00:25,196
Here, we have a person1 object with the

10
00:00:25,196 --> 00:00:27,378
name property set to 'Mary' and a getName

11
00:00:27,378 --> 00:00:30,222
function that just returns the value in

12
00:00:30,222 --> 00:00:33,651
name. Next, we have another person2 object

13
00:00:33,651 --> 00:00:37,026
with the name property set to 'John'. When

14
00:00:37,026 --> 00:00:39,276
we call bind on the person1.getName

15
00:00:39,276 --> 00:00:42,276
function and pass the person2 object as an

16
00:00:42,276 --> 00:00:44,651
argument, a new function is created and

17
00:00:44,651 --> 00:00:47,901
returned and assigned to the getNameCopy

18
00:00:47,901 --> 00:00:50,317
variable. Additionally, the value of this

19
00:00:50,317 --> 00:00:53,317
in the new function is set to the person2

20
00:00:53,317 --> 00:00:55,317
object. Therefore, when we print the

21
00:00:55,317 --> 00:00:58,651
output of the getNameCopy method call, it

22
00:00:58,651 --> 00:01:01,451
returns John. In this way, we are no

23
00:01:01,451 --> 00:01:03,651
longer changing the context of an existing

24
00:01:03,651 --> 00:01:06,651
function the way we did in call and apply,

25
00:01:06,651 --> 00:01:08,428
but instead create a new copy of a

26
00:01:08,428 --> 00:01:12,401
function and then change its context to

27
00:01:12,401 --> 00:01:15,317
another object. Let's look at the bind

28
00:01:15,317 --> 00:01:18,936
method in our demo. Again, this is our

29
00:01:18,936 --> 00:01:21,794
example from earlier. If we save the

30
00:01:21,794 --> 00:01:23,762
output from invoking the getNameCopy

31
00:01:23,762 --> 00:01:27,651
function, John is printed to the console.

32
00:01:27,651 --> 00:01:29,984
If we just print getNameCopy, it prints

33
00:01:29,984 --> 00:01:34,079
the body of the anonymous function to the

34
00:01:34,079 --> 00:01:35,000
console. Note, however, that this is a copy

