Understanding module caching and scopes

show more Understanding module caching and scopes provides you with in-depth training on Developer. Taught by Joseph LeBlanc as part of the Node.js Essential Training (2013) show less
please wait ...

Understanding module caching and scopes

The first time node loads the module through require, it caches the result. This can be beneficial, but it can also create problems if you don't plan ahead. In this video we'll look at when caching is helpful, and when it's necessary to work around. So, first let's take a look at an example of when caching can get you into trouble. Let's go to exercise files, and in chapter 4 in video 2, there's a start folder, just copy the start folder to the desktop. And then open the start folder in your favorite text editor.

Take a look first at app.js. Now on line nine, I'm defining a variable called another flight. Another flight is being set to the value of what's returned by require flight. I'm using the exact same code that I'm using on line one, so you might expect that we would get a fresh copy of the module in another flight. And consequently, calling getInfo on anotherFlight on line 11 here, should give us a blank set of values. So let's go to the command line and run this code, and see what happens.

Go to terminal, and from here, type CD space. And, then I'm just going to drag the start folder in the terminal, which will give me the path to that folder. So, now I'm going to type node space app.js. Hm. You'll see the exact same data that's being set in the first flight, is also seen in another flight. Here's what's happening. In Index.js, the three variables are being set on line 1. No matter how many times you require this module, it's still going to refer to these three variables.

Now this is going to be a problem if you want to use this as a reusable module for several different flights. So let's take a look at some other code that we can use that will help us keep separate instances of flight information. Go back to the exercise files, and then in chapter four, video two there's a snippets folder. I'm going to replace the files in the start folder with the ones in snippets. So first I'm going to copy over app.js and replace it. I'm going to do the same for the flight folder.

I'm going to copy index.js from snippets on over to flight and replace that file. Now I'm going to go up a level and up another level. And now I'm going to open the Start folder in Sublime Text again. So now let's have a look at the new index.js. This time instead of exporting multiple functions, I'm exporting one function. And I'm using the module exports constant to do that. Notice that all of the code is contained within one function. This function takes one argument, called info.

On lines 3 through 11, I'm defining an object called values. This object has place holders for the number, the origin, the destination. The departure time and the arrival time, as well as the actual departure time and the actual arrival time. Then starting on line 13, I have a loop. See as this loop is a bit of a mess, we want to make sure that we only get valid, predetermined object properties To do this, we need to loop over each property and make sure we only get the ones we've defined.

So in this case, we only want the properties seen on lines four through ten. So, we're going to loop over each one of those properties and then find those properties in the info object that gets passed in on line one. So if that property is found, we then set values to that property. Lines 19 through 29 define three functions that we're going to use. Notice this time we don't have any setters. The setter is effectively defined here on lines 13 through 17.

On lines 20 through 25 we have two functions, one for triggering the departure time and one for triggering the arrival time. These are currently just set to the current time stamp. And then finally, on lines 26 through 28, we're returning the values that get set. One thing that's a little interesting to note here is on line 31 we're returning the functions. We're not returning any of the values that are defined here on lines 3 through 11. The only way we can get these values is by using the get information function that's defined on lines 26 through 28 and that's going to be available through the functions that we return from this module.

Now let's have a look at app.js. On line 1, we're requiring the flight module. Then on line 3 through 7, we're defining a flight. We're able to set each individual property without using a function. Next on line 9, were defining a new variable. We're using the flight function that's returned from the module And passing in the information we're defining on line 3. Then on line 11 we're calling the trigger depart method on that object. And then finally, on line 13, we're calling the get information method of that object, and then outputting the results to the console.

On lines 15 through 23 we're doing the same process again only for a different flight. On this flight, we're not calling the triggerDepart method. And then finally on line 25, we're calling the getInformation method of the first object so we can prove that these are separate flights. Let's go to the command line and run this code now. Type in node app.js and then, hit return. You'll notice that, on the first object, the actual depart property got set to the current time stamp.

The second object is completely separate. It has a different number, origin, and destination. And then, finally, the original object is exactly the same as it was before. We've now successfully created two different objects. This all works because we're using separate function scopes. Each time this function gets called a new function scope is created and that way the data from each flight is kept separate. While modules are cached whenever they're loaded for the first time it's possible to use a function to create a new variable scope.

In the next video, we'll look at an even more flexible way of creating multiple objects from the same module.

Understanding module caching and scopes
Video duration: 6m 15s 3h 17m Intermediate


Understanding module caching and scopes provides you with in-depth training on Developer. Taught by Joseph LeBlanc as part of the Node.js Essential Training (2013)

JavaScript Node.js
please wait ...