There are three main reasons for using this kind of pattern;
Let's create a basic example application that reads in a couple of RSS feeds and shows them on two separate views, which have 2 separate JS files, and use the same context.
Create a new project in Ti Studio, call it whatever you want (I've called mine NamespaceMethod). Delete the pre-set code out of the app.js file, and then create two new files called feed1.js and feed2.js respectively, and add these to your Resources folder.
Now let's create the TableView for feed1.js...
...and feed2.js and save both of those files:
Run your app in the simulator now and you'll (hopefully) see a screen that looks like this:
Edit your app.js file by adding the following couple of lines to it, right after the RSSAPP declaration at the top. Note that I've used YQL to automatically do the XML Feed -> JSON conversion for us.
Now create the api.js file and save it to your Resources directory. Type or paste in the following code. This simple module just accepts a URL and creates a HTTPRequest and passes back the JSON-formatted response data via the 'success' function, or throws any errors back via the 'error' function.
Now we can call our "grabFeed" method from within the api.js file from anywhere in our app that utilizes the RSSAPP namespace. Let's populate the table1 object first - open up feed1.js and enter in the following code, just before the final line where you are adding the table1 object to the window. It should look like this:
Run your app in the simulator again and you should see that the first tableView (top of screen) is populated with RSS data from http://boydlee.com/feed.html.
As we're on mobile devices and we want to avoid using too much bandwidth (not to mention HTTPRequest collisions) we're going to populate the second table only when the first table has finished its data request/population. Add the following function call to feed1.js directly after the line "RSSAPP.table1.setData(rows);"
And now let's update feed2.js so it looks like the following:
Finally, run the app in your simulator. You should see table1 populated by data from http://boydlee.com/feed.html, via YQL, and when it has finished you'll see the data from the Appcelerator Developer Blog feed populate table2.
So what have we learnt from this sample? Firstly, you can see we no longer need to import the same file a hundred times - both the feed1.js and feed2.js files can access the webservice functions from api.js without multiple inclusions. This means less redundant code and less memory usage.
You also now have a convenient global structure - properties and objects added to the RSSAPP namespace can be accessed from within any part of your app at any time.
App-wide data can be instantiated once, and used over and over again. You can see this happening from the properties we created to hold the feed URL's - these were defined in the namespace from within app.js, and then accessed and passed around throughout our files.
Functions can now be defined as belonging to our namespace, meaning they can be executed from anywhere. You can see this happening when we call the loadFeed2Data function - it is declared within feed2.js but we're calling it directly from feed1.js, essentially removing the need for firing off multiple events or doing unnecessary Ti.include()'s.
Because all your windows and views exist in the same namespace they can be immediately instantiated - giving you faster app performance.
You can download the sample project here, and please comment if there's any mistakes in it (I wrote this fairly quickly!).