<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:copyright="http://blogs.law.harvard.edu/tech/rss" xmlns:image="http://purl.org/rss/1.0/modules/image/">
    <channel>
        <title>JavaScript</title>
        <link>http://www.codingmonk.com/category/6.aspx</link>
        <description>JavaScript.  Like you run in a browser.</description>
        <language>en-US</language>
        <copyright>Jim Fisher</copyright>
        <generator>Subtext Version 2.1.2.2</generator>
        <item>
            <title>Tips for Building a JavaScript Library</title>
            <link>http://www.codingmonk.com/archive/2008/08/17/tips-for-building-a-javascript-library.aspx</link>
            <description>&lt;p&gt;&lt;span style="FONT-FAMILY: Arial; FONT-SIZE: 10pt"&gt;You're an experienced developer. You learned a long time ago how to make your code generic and factor it for optimum reusability, neatly organizing it into tight, independent packages. Need a multithreaded web scrapper? You have a C# assembly that does that, thank you. Logging to the event viewer from T-SQL? No problem, there's that script you developed a few years ago. Want transformed text embedded in a graphic? Got it covered. The toolkit you've built over the years equips you to easily handle almost any task. &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-FAMILY: Arial; FONT-SIZE: 10pt"&gt;Almost. &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-FAMILY: Arial; FONT-SIZE: 10pt"&gt;Well… There is that bit of untidiness with your JavaScript. But everyone has that, right? I mean, with the hundreds of snippets smattered across the internet how could anyone keep those straight? Sure, you've made the obligatory attempt. You put the most often used routines into a single common include file. But the file grows with almost every new project, and each project leveraging this file uses only a fraction of its contents. The practice of monolithic includes is… unsatisfying. &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-FAMILY: Arial; FONT-SIZE: 10pt"&gt;Fear not. There is a better way. &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-FAMILY: Arial; FONT-SIZE: 10pt"&gt;&lt;strong&gt;About this Article&lt;/strong&gt; &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-FAMILY: Arial; FONT-SIZE: 10pt"&gt;This article describes some of the impediments that make fashioning and maintaining a library in JavaScript troublesome and discusses some techniques for surmounting these obstacles. These techniques have been adopted by CodingMonk and are manifest as the JACL (JavaScript Application Code Library) framework. As such, most of the examples are given in that context, yet the techniques described here lend themselves equally well to a personal implementation. &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-FAMILY: Arial; FONT-SIZE: 10pt"&gt;The code depicted here has been tested under both the Firefox and Internet Explorer browsers, including Microsoft's implementation of HTAs (HTML applications). I suspect that these techniques could also be ported to additional browsers (e.g. Opera, Safari) with minimal effort. &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-FAMILY: Arial; FONT-SIZE: 10pt"&gt;Before going further, I'd like to make it clear that my way is not the only way. There are a number of gifted JavaScript developers around the world and I expect there exist several additional techniques not covered here. Please keep in mind that what works well for me may not fit well in the preferences of another developer. &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-FAMILY: Arial; FONT-SIZE: 10pt"&gt;&lt;strong&gt;What Makes a Code Library? &lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-FAMILY: Arial; FONT-SIZE: 10pt"&gt;A fundamental preconception that people have when they hear the word "library" is that it is a collection of smaller parts. Although I can lump all of my scripts into a single reusable file and call it "Jim's library", that doesn't really hold true to the spirit of the word. Likewise, just as we wouldn't checkout every book from the shelves of a real library when researching English history, we shouldn't have to download the complete code when we're only using one or two of its features. &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-FAMILY: Arial; FONT-SIZE: 10pt"&gt;So, &lt;em&gt;our library should be organized into functional modules from which we can pick and choose what we need&lt;/em&gt;. &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-FAMILY: Arial; FONT-SIZE: 10pt"&gt;This deceptively simple diktat is absent from the majority of JavaScript "libraries" available on the internet. Why? Because modules inject unexpected complexity into a library, especially highly functional ones leveraging other modules as dependencies. We'll identify some for these complexities and address them now. When we're finished, we'll have a basis upon which to build a working, expandable framework for our JavaScript library. &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-FAMILY: Arial; FONT-SIZE: 10pt"&gt;&lt;strong&gt;Modules Including Modules &lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-FAMILY: Arial; FONT-SIZE: 10pt"&gt;As a code library matures, it's almost a given that one module will eventually need to include another. In JACL, for example, the Log module is automatically included by the Core module, so logging functionality is always available. Another example: each of JACL's self-updating controls leverages an AJAX implementation housed in the ServerRequest module. &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-FAMILY: Arial; FONT-SIZE: 10pt"&gt;So how do we include a JavaScript file from within another JavaScript file? Obviously the most common way to include JavaScript from HTML is with the script tag: &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN-LEFT: 36pt"&gt;   &lt;/p&gt;
&lt;p style="MARGIN-LEFT: 36pt"&gt;&lt;span style="FONT-FAMILY: Courier New; FONT-SIZE: 8pt"&gt;&amp;lt;script src="http://www.codingmonk.com/jacl/jacl.js"&amp;gt;&amp;lt;/script&amp;gt; &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;   &lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-SIZE: 10pt"&gt;&lt;span style="FONT-FAMILY: Arial"&gt;So from within a JavaScript file, one possibility is to leverage &lt;/span&gt;&lt;span style="FONT-FAMILY: Courier New"&gt;document.write()&lt;/span&gt;&lt;span style="FONT-FAMILY: Arial"&gt; to inject customized script tags whenever a library module needs to meet a dependency. A la: &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN-LEFT: 36pt"&gt;   &lt;/p&gt;
&lt;p style="MARGIN-LEFT: 36pt"&gt;&lt;span style="FONT-FAMILY: Courier New; FONT-SIZE: 8pt"&gt;document.write( "&amp;lt;script src='http://www.codingmonk.com/jacl/jacl.js'&amp;gt; &amp;lt;/script&amp;gt;");     &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;   &lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-FAMILY: Arial; FONT-SIZE: 10pt"&gt;One complication associated with dynamic inclusion of JavaScript: it is not immediately obvious when the included file has successfully loaded. Despite their similarities, it's easy to forget that the above two lines are not exactly equivalent. To be precise: &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;   &lt;/p&gt;
&lt;ul style="MARGIN-LEFT: 54pt"&gt;
    &lt;li&gt;&lt;span style="FONT-FAMILY: Arial; FONT-SIZE: 10pt"&gt;The first, declared within the HTML document, includes the JavaScript file while the page is first loading, but before scripts are run. &lt;/span&gt;&lt;/li&gt;
    &lt;li&gt;&lt;span style="FONT-FAMILY: Arial; FONT-SIZE: 10pt"&gt;The second, processed after the page has finished loading and declared scripts are running, identifies subsequent scripts that the browser should load when it gets the opportunity. &lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;   &lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-FAMILY: Arial; FONT-SIZE: 10pt"&gt;So as rule: JavaScript included from JavaScript is not immediately available, but JavaScript included from a tag in the unmodified HTML document is. &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-FAMILY: Arial; FONT-SIZE: 10pt"&gt;To demonstrate, we can do this: &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;   &lt;/p&gt;
&lt;p style="MARGIN-LEFT: 36pt"&gt;&lt;span style="FONT-FAMILY: Courier New; FONT-SIZE: 8pt"&gt;&amp;lt;html&amp;gt; &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN-LEFT: 36pt"&gt;&lt;span style="FONT-FAMILY: Courier New; FONT-SIZE: 8pt"&gt;&amp;lt;script src="http://www.codingmonk.com/jacl/jacl.js"&amp;gt;&amp;lt;/script&amp;gt; &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN-LEFT: 36pt"&gt;&lt;span style="FONT-FAMILY: Courier New; FONT-SIZE: 8pt"&gt;&amp;lt;script language = "JavaScript"&amp;gt; &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN-LEFT: 36pt"&gt;&lt;span style="FONT-FAMILY: Courier New; FONT-SIZE: 8pt"&gt;jacl.appMain = function(){alert("Something wicked this way comes...");}; &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN-LEFT: 36pt"&gt;&lt;span style="FONT-FAMILY: Courier New; FONT-SIZE: 8pt"&gt;&amp;lt;/script&amp;gt; &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN-LEFT: 36pt"&gt;&lt;span style="FONT-FAMILY: Courier New; FONT-SIZE: 8pt"&gt;&amp;lt;/html&amp;gt; &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;   &lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-FAMILY: Arial; FONT-SIZE: 10pt"&gt;But we can't we do this: &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;   &lt;/p&gt;
&lt;p style="MARGIN-LEFT: 36pt"&gt;&lt;span style="FONT-FAMILY: Courier New; FONT-SIZE: 8pt"&gt;&amp;lt;html&amp;gt; &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN-LEFT: 36pt"&gt;&lt;span style="FONT-FAMILY: Courier New; FONT-SIZE: 8pt"&gt;&amp;lt;script language="JavaScript"&amp;gt; &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN-LEFT: 72pt"&gt;&lt;span style="FONT-FAMILY: Courier New; FONT-SIZE: 8pt"&gt;document.write( "&amp;lt;script src='http://www.codingmonk.com/jacl/jacl.js'&amp;gt;&amp;lt;/script&amp;gt;"); &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN-LEFT: 72pt"&gt;&lt;span style="FONT-FAMILY: Courier New; FONT-SIZE: 8pt"&gt;jacl.appMain = function(){alert("Something wicked generates an error...");}; &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN-LEFT: 36pt"&gt;&lt;span style="FONT-FAMILY: Courier New; FONT-SIZE: 8pt"&gt;&amp;lt;/script&amp;gt; &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN-LEFT: 36pt"&gt;&lt;span style="FONT-FAMILY: Courier New; FONT-SIZE: 8pt"&gt;&amp;lt;/html&amp;gt; &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;   &lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-FAMILY: Arial; FONT-SIZE: 10pt"&gt;Because the second example produces an error message in the spirit of "jacl is undefined". &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-FAMILY: Arial; FONT-SIZE: 10pt"&gt;A more sophisticated alternative is to leverage the browser DOM, like so: &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN-LEFT: 36pt"&gt;   &lt;/p&gt;
&lt;p style="MARGIN-LEFT: 36pt"&gt;&lt;span style="FONT-FAMILY: Courier New; FONT-SIZE: 8pt"&gt;&amp;lt;html&amp;gt; &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN-LEFT: 36pt"&gt;&lt;span style="FONT-FAMILY: Courier New; FONT-SIZE: 8pt"&gt;&amp;lt;script language="JavaScript"&amp;gt; &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN-LEFT: 36pt"&gt;&lt;span style="FONT-FAMILY: Courier New; FONT-SIZE: 8pt"&gt;var o=document.createElement("script"); &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN-LEFT: 36pt"&gt;&lt;span style="FONT-FAMILY: Courier New; FONT-SIZE: 8pt"&gt;    o.src="http://www.codingmonk.com/jacl/jacl.js"; &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN-LEFT: 36pt"&gt;&lt;span style="FONT-FAMILY: Courier New; FONT-SIZE: 8pt"&gt;    o.onreadystatechange = function(){ &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN-LEFT: 108pt"&gt;&lt;span style="FONT-FAMILY: Courier New; FONT-SIZE: 8pt"&gt;if(this.readyState == "loaded"){ &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN-LEFT: 144pt"&gt;&lt;span style="FONT-FAMILY: Courier New; FONT-SIZE: 8pt"&gt;jacl.appMain = function(){alert("Something wicked this way comes...");}; &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN-LEFT: 108pt"&gt;&lt;span style="FONT-FAMILY: Courier New; FONT-SIZE: 8pt"&gt;} &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN-LEFT: 36pt"&gt;&lt;span style="FONT-FAMILY: Courier New; FONT-SIZE: 8pt"&gt;}; &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN-LEFT: 36pt"&gt;&lt;span style="FONT-FAMILY: Courier New; FONT-SIZE: 8pt"&gt;document.body.appendChild(o);     &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN-LEFT: 36pt"&gt;&lt;span style="FONT-FAMILY: Courier New; FONT-SIZE: 8pt"&gt;&amp;lt;/script&amp;gt; &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN-LEFT: 36pt"&gt;&lt;span style="FONT-FAMILY: Courier New; FONT-SIZE: 8pt"&gt;&amp;lt;/html&amp;gt; &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;   &lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-SIZE: 10pt"&gt;&lt;span style="FONT-FAMILY: Arial"&gt;Use of the DOM to dynamically load JavaScript provides a solution to our problem in the form of the &lt;/span&gt;&lt;span style="FONT-FAMILY: Courier New"&gt;onreadystatechange&lt;/span&gt;&lt;span style="FONT-FAMILY: Arial"&gt; event. This event fires only when the script has loaded completely, at which point we can reference functions and objects provided by the included file with impunity. &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-FAMILY: Arial; FONT-SIZE: 10pt"&gt;&lt;strong&gt;Initial Module Inclusion &lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-FAMILY: Arial; FONT-SIZE: 10pt"&gt;Despite the DOM example in the last section, the practice of including JavaScript from JavaScript gains us nothing when used where traditional HTML script tags are possible. If used appropriately, it enables us to manage our library entries and their dependencies intelligently, but it is still necessary to include the first module by hand. &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-FAMILY: Arial; FONT-SIZE: 10pt"&gt;This brings us full-circle to the first code snippet listed in this article: &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN-LEFT: 36pt"&gt;   &lt;/p&gt;
&lt;p style="MARGIN-LEFT: 36pt"&gt;&lt;span style="FONT-FAMILY: Courier New; FONT-SIZE: 8pt"&gt;&amp;lt;script src="http://www.codingmonk.com/jacl/jacl.js"&amp;gt;&amp;lt;/script&amp;gt; &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;   &lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-FAMILY: Arial; FONT-SIZE: 10pt"&gt;As it turns out, there is considerable usefulness in including an initial module as we would include a traditional script file. It provides for us select core routines without having to wait for a load event, including implementation of principals discussed above which load modules programmatically. Additionally, it gives us a place to perform library initialization. This can include, among other things, an application-defined entry point where we can be certain all library modules are loaded and ready. &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-FAMILY: Arial; FONT-SIZE: 10pt"&gt;To demonstrate, below is a working example of an HTML page leveraging JACL's Plot module to draw a sine wave. For those that are curious, it can cut-and-pasted into an html file and viewed it in any browser. &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN-LEFT: 36pt"&gt;   &lt;/p&gt;
&lt;p style="MARGIN-LEFT: 36pt"&gt;&lt;span style="FONT-FAMILY: Courier New; FONT-SIZE: 8pt"&gt;&amp;lt;html&amp;gt; &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN-LEFT: 36pt"&gt;&lt;span style="FONT-FAMILY: Courier New; FONT-SIZE: 8pt"&gt;&amp;lt;body&amp;gt; &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN-LEFT: 36pt"&gt;&lt;span style="FONT-FAMILY: Courier New; FONT-SIZE: 8pt"&gt;&amp;lt;script src="http://www.codingmonk.com/jacl/jacl.js"&amp;gt;&amp;lt;/script&amp;gt; &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN-LEFT: 36pt"&gt;&lt;span style="FONT-FAMILY: Courier New; FONT-SIZE: 8pt"&gt;&amp;lt;script language="JavaScript"&amp;gt; &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN-LEFT: 36pt"&gt;&lt;span style="FONT-FAMILY: Courier New; FONT-SIZE: 8pt"&gt;    jacl.use("jaclPlot"); &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN-LEFT: 36pt"&gt;&lt;span style="FONT-FAMILY: Courier New; FONT-SIZE: 8pt"&gt;    jacl.appMain=function(){ &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN-LEFT: 36pt"&gt;&lt;span style="FONT-FAMILY: Courier New; FONT-SIZE: 8pt"&gt;        jacl.plot.setOrigin(50,100); &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN-LEFT: 36pt"&gt;&lt;span style="FONT-FAMILY: Courier New; FONT-SIZE: 8pt"&gt;        jacl.log.info('Graphing sine wave...'); &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN-LEFT: 36pt"&gt;&lt;span style="FONT-FAMILY: Courier New; FONT-SIZE: 8pt"&gt;        for(var t=0;t&amp;lt; 300;t++) &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN-LEFT: 36pt"&gt;&lt;span style="FONT-FAMILY: Courier New; FONT-SIZE: 8pt"&gt;            jacl.plot.point(t,Math.sin(t/30)*30); &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN-LEFT: 36pt"&gt;&lt;span style="FONT-FAMILY: Courier New; FONT-SIZE: 8pt"&gt;        }; &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN-LEFT: 36pt"&gt;&lt;span style="FONT-FAMILY: Courier New; FONT-SIZE: 8pt"&gt;&amp;lt;/script&amp;gt; &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN-LEFT: 36pt"&gt;&lt;span style="FONT-FAMILY: Courier New; FONT-SIZE: 8pt"&gt;&amp;lt;/body&amp;gt; &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN-LEFT: 36pt"&gt;&lt;span style="FONT-FAMILY: Courier New; FONT-SIZE: 8pt"&gt;&amp;lt;/html&amp;gt; &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;   &lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-SIZE: 10pt"&gt;&lt;span style="FONT-FAMILY: Arial"&gt;Notice that our initial inclusion of the now familiar JACL Core module is done explicitly as an HTML tag. This ensures that the library's objects (in this case the &lt;/span&gt;&lt;span style="FONT-FAMILY: Courier New"&gt;jacl&lt;/span&gt;&lt;span style="FONT-FAMILY: Arial"&gt; object) are present, as are library defined code. In the JACL framework, this includes the &lt;/span&gt;&lt;span style="FONT-FAMILY: Courier New"&gt;use&lt;/span&gt;&lt;span style="FONT-FAMILY: Arial"&gt; method, which is simply a refined implementation of the DOM principals discussed in the previous section, and the&lt;/span&gt;&lt;span style="FONT-FAMILY: Courier New"&gt; appMain&lt;/span&gt;&lt;span style="FONT-FAMILY: Arial"&gt; method which is similar in principal to the &lt;/span&gt;&lt;span style="FONT-FAMILY: Courier New"&gt;onreadystatechange&lt;/span&gt;&lt;span style="FONT-FAMILY: Arial"&gt; event but fires only once when &lt;strong&gt;&lt;em&gt;all&lt;/em&gt;&lt;/strong&gt; modules have finally been loaded. &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-FAMILY: Arial; FONT-SIZE: 10pt"&gt;&lt;strong&gt;Avoiding Multiple Inclusions &lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-FAMILY: Arial; FONT-SIZE: 10pt"&gt;Another complexity inherent in the practice of &lt;em&gt;modules-including-modules&lt;/em&gt; occurs when two modules share the same dependency. If not handled, the same module can be included twice. The worst case scenario results in self-referencing recursion and a page that never finishes loading. &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-FAMILY: Arial; FONT-SIZE: 10pt"&gt;The simplest solution is to set a flag when a module is included for the first time, doing nothing on subsequent inclusions. By leveraging our object hierarchy, this is almost laughably easy as the individual objects created by a module act as flags simply by virtue of their existence: &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN-LEFT: 36pt"&gt;   &lt;/p&gt;
&lt;p style="MARGIN-LEFT: 36pt"&gt;&lt;span style="FONT-FAMILY: Courier New; FONT-SIZE: 8pt"&gt;if(jacl.plot==null) { //already created? &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN-LEFT: 36pt"&gt;&lt;span style="FONT-FAMILY: Courier New; FONT-SIZE: 8pt"&gt;    jacl.plot=new jacl_plot(); //no? then let's create an instance &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN-LEFT: 36pt"&gt;&lt;span style="FONT-FAMILY: Courier New; FONT-SIZE: 8pt"&gt;    // implementation goes here &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN-LEFT: 36pt"&gt;&lt;span style="FONT-FAMILY: Courier New; FONT-SIZE: 8pt"&gt;} &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;   &lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-FAMILY: Arial; FONT-SIZE: 10pt"&gt;The only drawback to this is that, while easy, the approach must be enforced across all library modules. A better alternative, one that can be implemented in the library object itself is to track which modules have been already been requested and don't add them via the DOM more than once. &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-FAMILY: Arial; FONT-SIZE: 10pt"&gt;&lt;strong&gt;Establishing a Library's Location &lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-FAMILY: Arial; FONT-SIZE: 10pt"&gt;Astute observers will notice one important nuance in the sine wave example: &lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN-LEFT: 36pt"&gt;   &lt;/p&gt;
&lt;p style="MARGIN-LEFT: 36pt"&gt;&lt;span style="FONT-FAMILY: Courier New; FONT-SIZE: 8pt"&gt;jacl.use("jaclPlot"); &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;   &lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-SIZE: 10pt"&gt;&lt;span style="FONT-FAMILY: Arial"&gt;The &lt;/span&gt;&lt;span style="FONT-FAMILY: Courier New"&gt;use&lt;/span&gt;&lt;span style="FONT-FAMILY: Arial"&gt; method does not require a fully qualified file path. At first glance this seems a trivial feature. After all, if we need to specify the URL when we include one module, it is only a small inconvenience to provide the URL with subsequent inclusions. The real problem arises, again, when modules include other modules. In the example where we included modules with the DOM, the path was hard-coded, as in: &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;   &lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-FAMILY: Courier New; FONT-SIZE: 8pt"&gt;o.src="http://www.codingmonk.com/jacl/jaclPlot.js"; &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;   &lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-FAMILY: Arial; FONT-SIZE: 10pt"&gt;This isn't going to do us any good if we try to access our library locally, or if we move it to a different domain. What we need is for the module to determine the library's location relative to the client. As it turns out, this isn't too difficult. Since we're including an initial core module with a fully qualified filename all we have to do search the document's script objects and parse the path from the correct one: &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;   &lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-FAMILY: Courier New; FONT-SIZE: 8pt"&gt;var path=""; &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-FAMILY: Courier New; FONT-SIZE: 8pt"&gt;for(t=0;t&amp;lt;document.scripts.length;t++){ &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-FAMILY: Courier New; FONT-SIZE: 8pt"&gt;        var val=document.scripts[t].outerHTML.toLowerCase(); &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-FAMILY: Courier New; FONT-SIZE: 8pt"&gt;        if(val.indexOf("jacl.js")==-1) continue; &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-FAMILY: Courier New; FONT-SIZE: 8pt"&gt;        var s=val.indexOf("src=\""); &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-FAMILY: Courier New; FONT-SIZE: 8pt"&gt;        if(s!=-1){ &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-FAMILY: Courier New; FONT-SIZE: 8pt"&gt;            s+=5; &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-FAMILY: Courier New; FONT-SIZE: 8pt"&gt;            path=val.substring(s,val.indexOf("jacl",s)); &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-FAMILY: Courier New; FONT-SIZE: 8pt"&gt;            break; &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-FAMILY: Courier New; FONT-SIZE: 8pt"&gt;        } &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-FAMILY: Courier New; FONT-SIZE: 8pt"&gt;    } &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;   &lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-SIZE: 10pt"&gt;&lt;span style="FONT-FAMILY: Arial"&gt;So the &lt;/span&gt;&lt;span style="FONT-FAMILY: Courier New"&gt;use&lt;/span&gt;&lt;span style="FONT-FAMILY: Arial"&gt; method knows to expand: &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-FAMILY: Courier New; FONT-SIZE: 8pt"&gt;jaclplot &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-FAMILY: Arial; FONT-SIZE: 10pt"&gt;into &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-FAMILY: Courier New; FONT-SIZE: 8pt"&gt;http://www.codingmonk.com/jacl/jaclplot.js&lt;/span&gt;&lt;span style="FONT-FAMILY: Arial; FONT-SIZE: 10pt"&gt; &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-FAMILY: Arial; FONT-SIZE: 10pt"&gt;by using the resulting path variable. Of course, storing the path as a global variable clutters the namespace. This is just one of several advantages of leveraging an object hierarchy for our JavaScript library… &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-FAMILY: Arial; FONT-SIZE: 10pt"&gt;&lt;strong&gt;Organizing a Hierarchy &lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-FAMILY: Arial; FONT-SIZE: 10pt"&gt;The best, most useful of libraries can suffer and die because it is not kept organized. As libraries grow, they become more difficult to keep arranged. No matter how many modules your library is broken down into, if the net result is just a chaotic bundle of routines, then the library hasn't helped as much as it could. &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-SIZE: 10pt"&gt;&lt;span style="FONT-FAMILY: Arial"&gt;Namespace hierarchies help us to stay organized. More than just factoring code into modules, this technique structures our code in precise and predictable ways. Notice from our examples how every use of the JACL library stems either directly or indirectly from the global &lt;/span&gt;&lt;span style="FONT-FAMILY: Courier New"&gt;jacl&lt;/span&gt;&lt;span style="FONT-FAMILY: Arial"&gt; object. JACL's implementation of the previous path-finding example populates a member variable rather than a global one: &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-FAMILY: Courier New; FONT-SIZE: 8pt"&gt;    jacl.path &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-FAMILY: Arial; FONT-SIZE: 10pt"&gt;By doing this, we need not worry ourselves with the possibility of naming collisions, where another module or otherwise appropriated script defines a variable named "path" too. &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-FAMILY: Arial; FONT-SIZE: 10pt"&gt;Continuing the review of the fully working sine-wave example given previously, notice that JACL's logging module adds an object which encapsulates various pieces of logging functionality: &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;   &lt;/p&gt;
&lt;p style="TEXT-ALIGN: justify"&gt;&lt;span style="FONT-FAMILY: Courier New; FONT-SIZE: 8pt"&gt;jacl.log.trivial("Here is a message"); &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;   &lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-FAMILY: Arial; FONT-SIZE: 10pt"&gt;Other JACL modules go to greater lengths to categorize their code: &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;   &lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-FAMILY: Courier New; FONT-SIZE: 8pt"&gt;jacl.input.textbox.applyEditMask(tbInput, "###-##-####"); &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;   &lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-FAMILY: Arial; FONT-SIZE: 10pt"&gt;How diverse you choose to make your hierarchy is, of course, a personal preference. &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-FAMILY: Arial; FONT-SIZE: 10pt"&gt;&lt;strong&gt;Review a real implementation &lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-FAMILY: Arial; FONT-SIZE: 10pt"&gt;As was pointed out numerous times, the techniques proposed in this article are more than theoretical. For a complete working example, one moderately more fleshed out, I highly recommend taking a look at the most recent implementation of Coding Monk's JACL framework: &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;   &lt;/p&gt;
&lt;p style="TEXT-ALIGN: center"&gt;&lt;a href="http://www.codingmonk.com/downloads/jacl.zip"&gt;&lt;span style="FONT-FAMILY: Arial; FONT-SIZE: 10pt"&gt;www.codingmonk.com/downloads/jacl.zip&lt;/span&gt;&lt;/a&gt;&lt;span style="FONT-FAMILY: Arial; FONT-SIZE: 10pt"&gt; &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;   &lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-FAMILY: Arial; FONT-SIZE: 10pt"&gt;Happy coding! &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;  &lt;/p&gt;&lt;img src="http://www.codingmonk.com/aggbug/21.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Jim Fisher</dc:creator>
            <guid>http://www.codingmonk.com/archive/2008/08/17/tips-for-building-a-javascript-library.aspx</guid>
            <pubDate>Mon, 18 Aug 2008 03:19:40 GMT</pubDate>
            <comments>http://www.codingmonk.com/archive/2008/08/17/tips-for-building-a-javascript-library.aspx#feedback</comments>
            <wfw:commentRss>http://www.codingmonk.com/comments/commentRss/21.aspx</wfw:commentRss>
        </item>
    </channel>
</rss>