1 00:00:00,200 --> 00:00:02,790 Now that we've seen how to create a closure. 2 00:00:02,790 --> 00:00:07,030 Let's apply that to our functions countBirds and countDogs. 3 00:00:08,110 --> 00:00:14,300 Firstly in the countBirds function, we want to make the count variable private. 4 00:00:16,990 --> 00:00:22,130 In other words, countDogs cannot access this variable. 5 00:00:22,130 --> 00:00:27,830 However, if we'd leave it like this, when we call countBirds, 6 00:00:27,830 --> 00:00:33,730 the count will reset to 0 every time it runs, and it will never get past 1. 7 00:00:33,730 --> 00:00:39,575 What we need to do is define an inner function inside of countBirds. 8 00:00:46,227 --> 00:00:51,341 Because the inner function is defined inside this function, 9 00:00:51,341 --> 00:00:56,158 it has access to the private scope of this outer function, 10 00:00:56,158 --> 00:00:58,830 including the count variable. 11 00:01:02,480 --> 00:01:06,400 Because the count variable is declared outside the inner function, 12 00:01:07,490 --> 00:01:11,920 the count won't reset every time the counter runs. 13 00:01:13,230 --> 00:01:14,753 Now we can return the counter function. 14 00:01:18,817 --> 00:01:22,850 Even though the countBirds has exited, the inner function, 15 00:01:25,716 --> 00:01:29,580 It returns, saves and uses this private scope. 16 00:01:33,225 --> 00:01:36,240 This return function forms our closure. 17 00:01:36,240 --> 00:01:39,740 I just want to make one small adjustment, 18 00:01:39,740 --> 00:01:44,143 often you'll see the function returned directly. 19 00:01:47,763 --> 00:01:51,269 So we can get rid of the name function of counter like this. 20 00:01:55,771 --> 00:01:59,735 Now because countBirds doesn't count birds anymore, 21 00:01:59,735 --> 00:02:03,260 it returns a bird counting function. 22 00:02:03,260 --> 00:02:06,250 Let's rename it to 23 00:02:08,180 --> 00:02:12,260 makeBirdCounter to keep our variable names more meaningful. 24 00:02:13,340 --> 00:02:14,012 Let's preview this now. 25 00:02:21,107 --> 00:02:26,387 Let's create a variable called birdCounter, 26 00:02:26,387 --> 00:02:33,780 and call our new closure producing function, makeBirdCounter. 27 00:02:36,440 --> 00:02:41,320 birdCounter now holds the counting function. 28 00:02:41,320 --> 00:02:41,820 Let's run it now. 29 00:02:47,257 --> 00:02:48,838 And we see it counts in upwards. 30 00:02:54,033 --> 00:02:59,210 And you'll see when we countDogs, it holds a separate global count. 31 00:03:01,610 --> 00:03:04,000 Even though this code works now, 32 00:03:04,000 --> 00:03:07,730 we know that using global variables is still a bad idea. 33 00:03:08,790 --> 00:03:12,965 Let's rewrite our dog counter the way we did the bird counter. 34 00:03:23,463 --> 00:03:26,741 We see we can completely eliminate the global variable, great. 35 00:03:34,550 --> 00:03:38,415 We're able to separate out two counters using closures. 36 00:04:01,637 --> 00:04:05,903 And notice how they both are using the same variable, count. 37 00:04:09,451 --> 00:04:14,426 Let's look at what happens when we create another birdCounter, birdCounter2. 38 00:04:22,589 --> 00:04:24,329 What do you think will happen when we call it? 39 00:04:30,967 --> 00:04:36,079 It has its own private scope as well, separate to birdCounter. 40 00:04:41,827 --> 00:04:47,451 Every time our counter maker function runs a new scope is created, 41 00:04:47,451 --> 00:04:55,620 and all those count variables are private values, because every scope is separate. 42 00:04:55,620 --> 00:04:59,905 We don't actually need two functions for our dog and bird counters. 43 00:05:06,650 --> 00:05:11,371 You can see that both are identical, except one uses 44 00:05:11,371 --> 00:05:16,210 the string birds and thee other uses the string dogs. 45 00:05:18,120 --> 00:05:23,130 If we could give the string to the closure instead of the counter function 46 00:05:24,350 --> 00:05:27,950 the inner function would be able to access that as well. 47 00:05:27,950 --> 00:05:32,505 So let's delete MakeDogCounter, and 48 00:05:32,505 --> 00:05:38,310 rename makeBirdCounter just to be makeCounter. 49 00:05:40,890 --> 00:05:45,887 Let's use a parameter to introduce another variable into the private scope, noun. 50 00:05:52,728 --> 00:05:55,360 Now we can count whatever we want. 51 00:05:57,670 --> 00:06:00,360 Let's see how to make a count for birds and 52 00:06:00,360 --> 00:06:03,325 dogs with our new improved makeCounter function. 53 00:06:03,325 --> 00:06:08,895 birdCounter is equal to makeCounter 54 00:06:08,895 --> 00:06:14,260 birds, and then dogCounter, 55 00:06:17,080 --> 00:06:20,790 makeCounter dogs. 56 00:06:20,790 --> 00:06:26,700 When we call makeCounter we can put it in a string, and then 57 00:06:26,700 --> 00:06:32,310 the function we get back is configured to the animal or thing we specify. 58 00:06:32,310 --> 00:06:33,400 Let's make sure it works. 59 00:06:44,817 --> 00:06:46,190 It does. 60 00:06:46,190 --> 00:06:47,674 We could count fish as well. 61 00:07:03,769 --> 00:07:07,154 And that's the first look at closures, in the next video, 62 00:07:07,154 --> 00:07:10,144 I'll show you where they are used in the real world.