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
Save to del.icio.us
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 |
Subscribe!
Auto Save Clipboard Images Redux
Extending SpiderMonkey JavaScript on Windows
Rhino JavaScript to EXE with launch4j
Compiling Rhino JavaScript to Java
Directory Traversal in Rhino JavaScript
A Command-Line MP3 Player for Windows
Auto Save Images from the Clipboard
Java in a Windows EXE with launch4j
An Interview with Tom Zimmer: Forth System Developer
Setting Windows Console Text Colors in C
Shrouding CSharp and Java Source Code with AWK
Scott Ballantyne: Blazin' Into Forth
Command-Line Image Format Conversion
Yet Another Enhanced Echo Command
Setting Text Color in a Batch File
Rhino JavaScript to EXE with launch4j
MicroISV on a Shoestring
DadHacker
The Bottom Feeder
Writin' That Code!
The Recursive ISV
The Thomsen Blog
Prototypically Speaking
The Reinvigorated Programmer