Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reference a JScript file from another one?

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?

like image 538
Thomas Svensen Avatar asked Jun 16 '10 14:06

Thomas Svensen


1 Answers

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.

like image 126
Cauterite Avatar answered Nov 16 '22 03:11

Cauterite