Originally published on: Wed, 28 Apr 2010 00:09:54 +0000
I like to separate JavaScript functions into libraries that I can later reference in some other script.
It's easy enough to reference these in HTML using the SRC attribute of the SCRIPT tag.
I the case of WSH, one can use a WSF document to dynamically construct a composite script via combinations of inline and/or external files. However, I prefer a more simple approach when I'm using WSH.
Please consider these two files:
one.js
inc.js
The first couple of lines of one.js create a new FileSystemObject. Then, the dot operator is used to immediately refer to that object so that we can invoke OpenTextFile() to open a file called inc.js for mode 1 ( reading ).
Again, the dot operator is used to refer to the new object so that we can call the ReadAll() method to load the entire inc.js file into a string.
Finally, we call eval() to dynamically execute the contents of inc.js, effectively simulating an "include" operation available natively in other scripting languages.
In the inc.js file, I've chosen to use an alternate form of function definition that ensures that the named function will appear globally so that it can be referenced by other included modules.
We prove that we've invoked the included file which defines a function called display() by invoking this new function.
If you execute one.js via the following command-line:
cscript /nologo one.js
...you should see the output: Hello!
If you have a number of files to include, you might not want to keep creating FileSystemObject instances. We can create a library that encapsulates this behavior into a function and can then use the technique from one.js to dynamically evaluate that library.
includelib.js
This library creates one global instance of FileSystemObject, incfso, for future reference.
By dynamically evaluating includelib.js, we now have a callable include() function. We can then include inc.js and can invoke the display method again.
two.js
When you execute two.js: cscript /nologo two.js
...you should see the text: Hello, again.
Unless otherwise noted, all code and text entries are Copyright ©2010 by James K. Lawless
Views expressed in this blog are those of the author and do not necessary reflect those of the author's employer. Views expressed in the comments are those of the responding individual.

Save to StumbleUpon
Digg it
Save to Reddit
Share on Facebook
Share on Twitter
More bookmarks
| Previous post: | An Interview with Brad Templeton |
|---|---|
| Next post: | An Interview with the Creator of the BDS C Compiler |
Click **here**
A JavaScript REPL for Android Devices
A Review of Kevin Mitnick's Book Ghost in the Wires
Play MP3 Files with Python on Windows
Preserving my Favorite HN Links
COM Scripting in C by way of JavaScript
Changing the C64 Text Color in C
Mad Schemes : Learning Lisp via SICP
A TCP Command Line Interface in Rhino JavaScript
Screen Capture from Multiple Monitors in Java
Why Some Web Sites will go Dark on Jan 18th
Book Review : Paull Allen - Idea Man
A 90's Experiment in Online Systems - The U.S. West CommunityLink Service