How to reference a JScript file from another one?

前端 未结 3 1764
囚心锁ツ
囚心锁ツ 2020-12-15 09:56

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 librar

3条回答
  •  旧巷少年郎
    2020-12-15 10:17

    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.

提交回复
热议问题