Originally published on: Wed, 28 Apr 2010 00:09:54 +0000
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:
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.
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.
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.
|Previous post:|| An Interview with Brad Templeton|
|Next post:||An Interview with the Creator of the BDS C Compiler|
About Jim ...
Jim Lawless' Assembly Language Blog