Viewers: in countries Watching now:
While it's possible to get around module caching by using function scopes, it isn't the most flexible solution. In this video, we'll rework a module to generate objects. Let's start with the code from the previous video. Go to exercise files and chapter four, video three, and then copy the Start folder to the desktop. Now open the Start folder in a text editor. I'm going to go to Flight and index.js. In here, we have our current module, and it's exporting one function, and that function is creating a scope where we're holding all the variables.
What I'd like to do is create another function that will serve as the base for new objects. Let's do that now. Make some space at the top and then type var Flight, with a capital F, equals function. Don't define any arguments for this function, and clean out the body. Notice that we're using a capital F for flight. This is just a reminder that we're using this as the base for new objects. Now let's start adapting our old code into this new function. First copy everything from line 7 to line 15, and paste it on line 2.
Now change it to this values. Instead of this values, I'm also going to change it to this data. Next, we're going to create a function that will perform what's currently happening on lines 25 through 29. I'm going to call this function fill. So copy lines 25 through 29 and then make some space in the flight function. And then define this.fill. Set this to a function, and have it take one argument. This argument will be named info.
Now paste in the code you copied a moment ago. Before we leave this function, one more thing we need to do is change values to this.data. Next, we need to pull in the old functions that we were returning previously. Go down to line 42 and copy everything from 42 to 50, and now paste all that on line 20. This time we're going to prefix all of the function names with this. And again we need to change values to this.data. Finally, we need to change the module exports function.
Clear out everything in the current function. So now what we want this module export's function to do is to create a new instance of the flight object. To do this, we're going to create a new variable. Start by typing Var. And I'm going to call this variable instance. And I'm going to set it to a new copy of flight. Next, I'm going to fill this new object with all the information that's getting passed in through the module exports function. And then finally, I'm going to return the instance.
After saving the file, let's go to terminal. Type in cd, and then I'm going to drag the start folder into terminal and that will give me the path. And if I list the directory contents I have app.js and flight. app.js is currently defining two different flights and it's outputting the first flight, then defining the second one, outputting it, and then outputting the first one again. So let's run this example. Type in node space app.js and then hit Return.
So this example is still returning separate instances for each flight, and this is exactly what we want. Now there's one thing to note about the function that we're exporting here, in index.js. This function may seem a bit redundant at first, but it's actually enforcing a consistent way of getting new objects. This extra function is serving as a factory. A factory is a design pattern for creating instances of objects. When you provide a factory, the end programmer doesn't have to think about how to set up an object, because the factory handles this.
It's also possible to return more than one type of object from a factory, based on whatever criteria you want. In the example here of a flight, we may want this factory to also return itineraries. It could detect if a destination is a string or an array, then return the right type of object based on that information. There may also be some scenarios where you really don't want to use a factory. In this case, you want to export the function itself rather than a factory function. I've included a no-factory version of both the app and the module in the Examples folder.
So in this example, instead of setting module exports to a function that acts as a factory, we're setting it to flight. Then in the application we're using the new operator to create new flight objects with that function. There are different ways of getting around module caching, depending on how you want to protect code and enforce consistency. Factories can streamline object creation and ensure separate instances on each call. In the next chapter, we'll start using the express framework to build an application.
In the meantime, I have a challenge in the next video, to help you get more hands on experience with modules.
Find answers to the most frequently asked questions about Node.js Essential Training .
Here are the FAQs that matched your search "" :
Sorry, there are no matches for your search "" —to search again, type in another word or phrase and click search.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.