如何从另一个 JScript 文件引用一个 JScript 文件?
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试使用 Windows 脚本文件。它基本上是一个 XML 文档,允许您包含多个脚本文件并定义多个作业等。
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.
基于托马斯的解决方案——这是一个类似但更模块化的方法。首先,要调用的脚本:
然后,在调用脚本中: 以
这种方式定义的库不会生成或依赖任何上值,但
eval
将返回从周围的匿名函数接收的任何值在图书馆里。Based on Thomas's solution — here's a similar, but more modular approach. First, the script to call:
Then, in the calling script:
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.好的,我找到了一个不错的解决方案:
我创建一个可以附加我的库函数的对象。这样,我可以从调用脚本访问我的库中定义的代码。不完美,但还算可以接受。
很高兴看到更合适的解决方案:-)
OK, I found a decent solution:
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 :-)