WEBVTT 00:06.050 --> 00:13.520 In this video you're going to get introduced to modules and file exports so you're gonna use those to 00:13.640 --> 00:20.930 be able to exchange the variables and functions or methods between different javascript points. 00:21.560 --> 00:31.840 So I've created a folder file exports inside the node folder so let's move to that folder inside our 00:31.840 --> 00:32.690 terminal. 00:32.770 --> 00:39.800 So I'm going to go up one directory so this is where we are. 00:40.180 --> 00:44.070 And then I'm going to go to file exports. 00:44.220 --> 00:44.890 All right. 00:44.920 --> 00:54.310 So now I'm going to create a new file and call it up those deaths and then I'm going to create another 00:55.030 --> 01:01.080 or a new folder and call it live for library. 01:01.080 --> 01:07.830 So basically inside our library we're gonna have some JavaScript files with some variables and functions 01:08.100 --> 01:12.240 which we need inside up daughter day as. 01:12.600 --> 01:19.290 So first let's get introduced to the module variable 01:22.170 --> 01:30.480 so if you console the log module let's see what we get by the way you can do this in any javascript 01:30.480 --> 01:31.040 file. 01:31.440 --> 01:35.610 So I'm going to run node up most both. 01:35.880 --> 01:42.410 And here is our module objects. 01:42.540 --> 01:51.150 So what you are interested in is the exports property which will show you all the variables and functions 01:51.480 --> 01:54.460 which you exported from other files to this file. 01:55.260 --> 01:56.460 So at the moment it's empty. 01:57.550 --> 01:58.140 All right. 01:58.200 --> 02:02.460 So let's say that we want to use a function greeting 02:05.290 --> 02:08.350 which is not defined inside this file. 02:08.710 --> 02:15.180 So is going to be inside a file located in the library. 02:15.300 --> 02:20.970 So let's create a new file inside the library and call Callie's greeting those checks. 02:24.360 --> 02:28.230 So inside this file we're going to 02:32.540 --> 02:41.190 create a function which we will expose to up those days so in this example we're going to export one 02:41.190 --> 02:41.650 function. 02:43.470 --> 02:53.840 So what you can do you can access the module objects inside this file and then type exports and said 02:53.840 --> 02:59.880 that to one function because you want to export only one function. 02:59.880 --> 03:04.140 So let's say that our function is going to console the log. 03:05.070 --> 03:07.300 Hello. 03:07.430 --> 03:07.890 All right 03:12.050 --> 03:19.640 so now if you go to up those days we need to add another line to say that we're going to be importing 03:20.690 --> 03:33.730 the function exported from greeting those days and I'm going to call it greeting. 03:33.820 --> 03:47.130 So the syntax we need to use here is going to be require followed by the location of the file greeting 03:47.160 --> 03:48.410 vote yes. 03:48.830 --> 03:52.820 So I'm going to place everything inside quotes. 03:53.000 --> 03:59.870 So we starting from the current location and then we're going to the library folder and then greeting 04:00.080 --> 04:02.780 those Jess. 04:02.850 --> 04:03.540 All right. 04:03.540 --> 04:06.230 So now let's try this. 04:06.300 --> 04:13.910 So if you run the node command we can see Hello. 04:13.920 --> 04:16.850 Meaning that this function is working. 04:17.310 --> 04:22.070 Let's have a look at our module variable. 04:22.290 --> 04:27.170 So there are no exports here because we're not exporting anything from uploading. 04:27.340 --> 04:37.980 But if you run this on greasing lodges and you console that log the module 04:43.840 --> 04:49.590 variable then this time you will see that we've got something inside the exports. 04:49.630 --> 04:51.400 You've got a function. 04:51.400 --> 04:53.210 Perfect. 04:53.230 --> 05:02.070 Basically we are exporting an anonymous function and that's why you see once if you call this function 05:02.250 --> 05:05.580 something like greets 05:16.830 --> 05:18.150 then this will change. 05:18.160 --> 05:18.800 So let's have a look. 05:21.790 --> 05:24.970 You can see it's a function and it's called greets. 05:25.080 --> 05:25.390 All right. 05:25.400 --> 05:26.540 Perfect. 05:27.110 --> 05:36.190 So now let's create another example so we're gonna create another file inside the library and we're 05:36.200 --> 05:39.110 going to call its school those deaths. 05:39.590 --> 05:49.560 So inside this file we're going to have another objects or function which we will export to up in those 05:49.560 --> 05:51.270 chairs. 05:51.360 --> 06:00.260 So this time we're gonna go straight away to the module variable the exports property and we're going 06:00.260 --> 06:05.800 to add things manually so this is where we are at the moment. 06:05.830 --> 06:06.430 All right. 06:06.520 --> 06:19.530 So we're just gonna add for example an average function which will just calculates the average of grades 06:20.040 --> 06:22.260 which can be found inside an array. 06:22.350 --> 06:30.950 So we give it an array and then it will calculate the average for us and returns. 06:31.380 --> 06:39.880 So basically what we need to do we're going to create a variable some which is initiated to 0. 06:40.750 --> 06:42.940 And then for each element of the array 06:53.460 --> 07:03.530 we're going to add the value of the variable or the elements to the sum variable like that's quite easy 07:15.530 --> 07:16.300 okay. 07:16.420 --> 07:26.930 And then we're going to return the sum divided by the number of grades which which is gonna be the length 07:26.960 --> 07:35.500 of the array Okay so let's try this. 07:35.500 --> 07:41.080 Now we exporting this from school not today has to up those days. 07:41.230 --> 07:45.920 So we need to add another line to up those stairs. 07:46.030 --> 07:48.370 Right. 07:48.370 --> 07:48.910 So 07:52.050 --> 07:55.320 let's call it average 07:58.130 --> 08:04.350 and again we're gonna use require. 08:04.590 --> 08:11.430 We're gonna need the location of our file school both days which is gonna be lib. 08:11.430 --> 08:14.410 School board chance. 08:14.600 --> 08:22.290 Now the difference between that imbalance is that in that case we are exporting a function from greeting 08:22.350 --> 08:23.880 those chairs like that's 08:29.220 --> 08:35.820 and we setting the whole exports property of the module variable to that function. 08:35.820 --> 08:38.910 Now in this scenario things are slightly different. 08:39.330 --> 08:45.600 We are just appending average to exports. 08:45.600 --> 08:59.490 So basically you still need to say that you require that file dots average and this will import the 08:59.910 --> 09:03.090 average function successfully for it for you. 09:03.090 --> 09:11.190 So what you can do you can try those console the long average of these grades. 09:11.190 --> 09:15.110 So for example let's say those we've got 09:18.650 --> 09:25.390 70 55 90 and 100. 09:25.900 --> 09:27.410 And let's see what we get 09:35.770 --> 09:36.030 right. 09:36.030 --> 09:38.860 So we're getting seventy eight point seventy five. 09:40.010 --> 09:44.010 So let's try something else. 09:44.450 --> 09:45.760 So let's try 09:53.250 --> 09:57.980 eighty nine point one four for example. 09:59.610 --> 10:01.080 OK just to show you what happens 10:04.120 --> 10:04.370 OK. 10:04.380 --> 10:09.420 So now we're getting at this number and let's say that you would like to show only two digits after 10:09.420 --> 10:10.230 the decimal. 10:10.320 --> 10:12.480 So you can just add 10:16.630 --> 10:18.910 two fixed 10:21.810 --> 10:26.130 and plus two to the method. 10:26.130 --> 10:26.570 All right. 10:26.760 --> 10:29.370 So let's try again. 10:29.550 --> 10:30.150 And here we go 10:33.790 --> 10:34.460 there. 10:34.500 --> 10:42.380 That's how you attach a function to the exports property of the module objects. 10:42.390 --> 10:46.980 Now let's add something else. 10:46.980 --> 11:00.150 So this time I'm going to add an object grades for example and let's say that it's called a best property 11:01.710 --> 11:06.540 and an average property. 11:07.110 --> 11:07.350 OK 11:10.220 --> 11:14.800 so let's check the module variable and see what we've got 11:24.210 --> 11:24.990 in the exports. 11:24.990 --> 11:32.260 You can see that we've got a function and a grades object. 11:32.660 --> 11:32.880 Right. 11:32.880 --> 11:43.290 So now let's go to the up votes file and let's access the objects that we have just exported so let's 11:43.290 --> 11:44.960 log it. 11:45.060 --> 11:55.770 So again to access it we're going to require the same file and then since we attached grades to the 11:56.070 --> 12:02.370 exports property then we could just access it like this. 12:02.460 --> 12:03.660 So let's run 12:06.520 --> 12:07.770 up more ideas. 12:09.320 --> 12:10.270 And here we go. 12:10.360 --> 12:14.300 That's our grades object. 12:14.320 --> 12:24.880 So hopefully you've got now a good understanding of how to export objects and functions or methods between 12:26.090 --> 12:27.560 different JavaScript files. 12:28.730 --> 12:34.780 So in the next video you're gonna start looking at built in modules in new days. 12:34.800 --> 12:38.090 Now you're going to start with the HP module.