如何在另一个不在浏览器内运行的 JavaScript 文件中包含 JavaScript 文件?

如何在另一个不在浏览器内运行的 JavaScript 文件中包含 JavaScript 文件?

问题描述:

我知道以前有人问过很多类似的问题.与我的情况不同的是,我使用的是 Windows Scripting Host 并从 DOS 命令行运行脚本,而不是从浏览器内部运行.

I know that many similar questions have been asked before. The difference in my case is that I am using Windows Scripting Host and am running the script from the DOS command line, not from inside a browser.

我能想到的唯一方法是使用 fso.OpenTextFile(ThisFile, 1) 从磁盘读取文件作为文本,然后使用 eval 函数将文本视为代码.

The only way I can think of doing this is to read the file from the disk as text using fso.OpenTextFile(ThisFile, 1), and then using the eval function to treat the text as code.

有没有更方便的方法?

据我所知,不,这是唯一真正的选择.示例:

As far as I can tell, no, that's the only real option. Example:

text.js:

var fso = new ActiveXObject("Scripting.FileSystemObject");
var ts = fso.OpenTextFile("foo.js");
var script = ts.ReadAll();
eval(script);
foo();
WScript.Echo(bar);

foo.js:

var bar = "testing";
function foo() {
    WScript.Echo("foo");
}

输出:

foo
testing