如何从另一个 JScript 文件引用一个 JScript 文件?

发布于 2024-09-05 12:54:52 字数 380 浏览 7 评论 0原文

我正在使用 JScript 和 WSH 编写一些服务器端脚本。脚本变得相当长,一些常见的函数和变量更适合我包含在各种脚本实例中的通用库脚本。

但是,我找不到从另一个 JScript 文件引用一个 JScript 文件的方法。有一段时间,我认为读取文件内容并将其传递给 eval() 可以工作。但是,正如 MSDN 上所说:

请注意,eval 语句中定义的新变量或类型对于封闭程序不可见。

有什么方法可以包含/引用另一个 JScript 文件吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

甲如呢乙后呢 2024-09-12 12:54:52

尝试使用 Windows 脚本文件。它基本上是一个 XML 文档,允许您包含多个脚本文件并定义多个作业等。

<!-- MyJob.wsf -->
<job id="IncludeExample">
  <script language="JScript" src="MyLib1.js"/>
  <script language="JScript" src="MyLib2.js"/>
  <script language="JScript">
    WScript.Echo(myLib1.foo());
    WScript.Echo(myLib2.bar());
  </script>
</job>

Try using a Windows Script File. It's basically an XML document which allows you to include multiple script files and define multiple jobs, amongst other things.

<!-- MyJob.wsf -->
<job id="IncludeExample">
  <script language="JScript" src="MyLib1.js"/>
  <script language="JScript" src="MyLib2.js"/>
  <script language="JScript">
    WScript.Echo(myLib1.foo());
    WScript.Echo(myLib2.bar());
  </script>
</job>
飘逸的'云 2024-09-12 12:54:52

基于托马斯的解决方案——这是一个类似但更模块化的方法。首先,要调用的脚本:

/* include.js */
(function () {
    var L = {/* library interface */};
    L.hello = function () {return "greetings!";};
    return L;
}).call();

然后,在调用脚本中: 以

var Fs = new ActiveXObject("Scripting.FileSystemObject");
var Lib = eval(Fs.OpenTextFile("include.js", 1).ReadAll());
WScript.echo(Lib.hello()); /* greetings! */

这种方式定义的库不会生成或依赖任何上值,但 eval 将返回从周围的匿名函数接收的任何值在图书馆里。

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.

人生百味 2024-09-12 12:54:52

好的,我找到了一个不错的解决方案:

// A object to which library functions can be attached
var library = new Object;
eval((new ActiveXObject("Scripting.FileSystemObject")).OpenTextFile("common_script_logic.js", 1).ReadAll());

// Test use of the library
library.die("Testing library");

我创建一个可以附加我的库函数的对象。这样,我可以从调用脚本访问我的库中定义的代码。不完美,但还算可以接受。

很高兴看到更合适的解决方案:-)

OK, I found a decent solution:

// A object to which library functions can be attached
var library = new Object;
eval((new ActiveXObject("Scripting.FileSystemObject")).OpenTextFile("common_script_logic.js", 1).ReadAll());

// Test use of the library
library.die("Testing library");

I create an object to which I can attach my library functions. That way, I can reach code defined in my library from the calling script. Not perfect, but quite acceptable.

It would be great to see a more proper solution :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文