I am writing some server-side scripts using JScript and WSH. The scripts are getting quite long, and some common functions and variables would fit better in a general library script which I included in my various script instances.
But, I cannot find a way reference one JScript file from another. For a moment, I though reading the file contents and passing it to eval()
could work. But, as it says on MSDN:
Note that new variables or types defined in the eval statement are not visible to the enclosing program.
Is there any way to include/reference a JScript file from another one?
Based on Thomas's solution — here's a similar, but more modular approach. First, the script to call:
/* include.js */
(function () {
var L = {/* library interface */};
L.hello = function () {return "greetings!";};
return L;
}).call();
Then, in the calling script:
var Fs = new ActiveXObject("Scripting.FileSystemObject");
var Lib = eval(Fs.OpenTextFile("include.js", 1).ReadAll());
WScript.echo(Lib.hello()); /* greetings! */
Libraries defined this way don't produce or rely on any upvalues, but the eval
will return any value it receives from the surrounding anonymous-function in the library.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With