1 00:00:00,000 --> 00:00:00,220 2 00:00:00,220 --> 00:00:04,050 >> TEACHER: So, in geolocation zero, I declared two functions, one called 3 00:00:04,050 --> 00:00:08,070 geolocate which geolocates the user, and one called call back which was the 4 00:00:08,070 --> 00:00:11,730 function that geolocate called ultimately by way of get current 5 00:00:11,730 --> 00:00:14,630 position once the browser had figured out where the user is. 6 00:00:14,630 --> 00:00:18,400 Now, given that I'm only calling this function once, and it only exists to 7 00:00:18,400 --> 00:00:21,450 be called by get current position, technically, there's no reason that I 8 00:00:21,450 --> 00:00:24,640 had to declare it as a separate function and give it its own name. 9 00:00:24,640 --> 00:00:27,800 Rather, I could have simply implemented an anonymous function, 10 00:00:27,800 --> 00:00:31,780 otherwise known as a lambda function, effectively passing in the code that I 11 00:00:31,780 --> 00:00:35,990 want to be executed after get current position has found the user. 12 00:00:35,990 --> 00:00:38,730 >> In particular, I'd like to have done the following. 13 00:00:38,730 --> 00:00:41,550 First, let's highlight and cut this line of code here in my callback 14 00:00:41,550 --> 00:00:43,240 function so as to use it later. 15 00:00:43,240 --> 00:00:45,960 But let's get rid of the callback function all together. 16 00:00:45,960 --> 00:00:49,780 Now, inside of the geolocate function, let's highlight and delete the name of 17 00:00:49,780 --> 00:00:53,190 the function that I was previously calling, and instead, declare an 18 00:00:53,190 --> 00:00:56,090 anonymous function with no name that still takes an 19 00:00:56,090 --> 00:00:57,940 argument called Position. 20 00:00:57,940 --> 00:01:01,240 And then after this, by convention, let's put an open curly brace. 21 00:01:01,240 --> 00:01:04,250 Down here let's put the closing curly brace and the closing parenthesis and 22 00:01:04,250 --> 00:01:05,170 a semicolon. 23 00:01:05,170 --> 00:01:07,940 >> And then inside of this otherwise anonymous function, let's paste in the 24 00:01:07,940 --> 00:01:11,020 line of code that we'd still like to be called. 25 00:01:11,020 --> 00:01:14,180 Ultimately, then, all I've done is replace a named function with an 26 00:01:14,180 --> 00:01:17,030 anonymous function but have not changed the page's functionality. 27 00:01:17,030 --> 00:01:20,260 But this is arguably a bit cleaner because I'm not littering my code with 28 00:01:20,260 --> 00:01:22,230 unnecessarily defined functions. 29 00:01:22,230 --> 00:01:26,890 Rather, I'm only defining the code I want to be executed exactly where it's 30 00:01:26,890 --> 00:01:27,700 meant to be called. 31 00:01:27,700 --> 00:01:30,510 >> Let's save the file and open the page in a browser. 32 00:01:30,510 --> 00:01:39,330 http://localhost /geolocation1/html And there I still am, in Cambridge, 33 00:01:39,330 --> 00:01:41,130 Massachusetts. 34 00:01:41,130 --> 00:01:42,380 Anonymously. 35 00:01:42,380 --> 00:01:44,000