如何在单独的文件中创建 javascript 库并“包含” 它在另一个?

发布于 2024-07-21 08:59:14 字数 443 浏览 9 评论 0原文

首先,需要注意的是。 主脚本不在网页中运行。 我将使用 Windows Script Host 在 Windows 中运行 .js 文件。

问题: 我想创建一个包含许多对象的javascript“库”,每个对象都有许多函数。 我预计这个库会随着时间的推移而变得相当大,并希望将其保存在一个单独的 javascript 文件中(我们称之为 Library.js)。 我想从另一个脚本访问 Library.js 中的对象(我们将其称为 User.js)。

本质上,我正在寻找类似于 C/C++“包含”范例的东西。

有没有办法用javascript实现这个? (请记住,这不是基于网络的)

First, a caveat. The main script is not run in a webpage. I will be running the .js file in Windows using Windows Script Host.

The problem:
I would like to create a javascript "library" containing a number of objects, each with a number of functions. I expect this library will get rather large with time and would like to keep it in a separate javascript file (let's call it Library.js). I would like to access the objects from Library.js from another script (let's call this one User.js).

In essence, I am looking for something similar to the C/C++ "include" paradigm.

Is there anyway to implement this in javascript? (Remember, this is not web-based)

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

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

发布评论

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

评论(3

薆情海 2024-07-28 08:59:14

此处的页面是我能找到的最接近的页面。 它讨论了 .wsf 文件,它允许您将多个脚本(可能用不同语言编写)合并到一个文件中。

对于你的问题,它应该是这样的:

user.wsf

<job id="IncludeExample">
   <script language="JScript" src="library.js"/>
   <script language="JScript">
      <![CDATA[
      // use your library functions here
      ]]>
   </script>
</job>

可能有更好的方法,但我不是 WSH 专家。

This page right here is the closest I could find. It talks about .wsf files which let you combine multiple scripts (that may be written in different languages) in one file.

For your question it should be something like:

user.wsf

<job id="IncludeExample">
   <script language="JScript" src="library.js"/>
   <script language="JScript">
      <![CDATA[
      // use your library functions here
      ]]>
   </script>
</job>

There may be better ways, but I'm not a WSH expert.

美人骨 2024-07-28 08:59:14

我知道这是一个老问题,并且已经得到回答和接受。

我了解 .WSF 文件及其工作原理; 他们服务于目的。

然而,我还发现在 WSH 中运行的纯 .js 文件中进行“包含”很方便。 这是我的解决方案。

    function includeFile (filename) {
        var fso = new ActiveXObject ("Scripting.FileSystemObject");
        var fileStream = fso.openTextFile (filename);
        var fileData = fileStream.readAll();
        fileStream.Close();
        eval(fileData);
    }

定义了该实用函数后,我可以在 javascript 模块内执行此操作:

includeFile("externalFile1.js");
includeFile("externalFile2.js");
includeFile("etc.js");

...然后我可以调用任何函数或触发这些模块中定义的任何变量。

类似的技术适用于 .vbs 文件:
怎么做我在 VBScript 中包含一个公共文件(类似于 C #include)?

I know this is an old question and it has been answered and accepted.

I know about .WSF files and how they work; they serve the purpose.

However, I've also found it handy to do the "inclusion" from within a pure .js file that runs in WSH. Here's my solution for that.

    function includeFile (filename) {
        var fso = new ActiveXObject ("Scripting.FileSystemObject");
        var fileStream = fso.openTextFile (filename);
        var fileData = fileStream.readAll();
        fileStream.Close();
        eval(fileData);
    }

With that utility function defined, I can do this from within a javascript module:

includeFile("externalFile1.js");
includeFile("externalFile2.js");
includeFile("etc.js");

...and then I can invoke any of the functions or tickle any of the variables defined in those modules.

A similar technique works with .vbs files:
How do I include a common file in VBScript (similar to C #include)?

眼藏柔 2024-07-28 08:59:14

您可以通过使用 Windows 脚本宿主而不是特定的 .js 或 .vbs 文件来实现您的目标。 有关详细信息,请访问MSDN 站点

从链接:

Windows 脚本 (*.wsf) 文件是文本
包含可扩展标记的文档
语言 (XML) 代码。 它包含
为您提供的多项功能
增加了脚本编写的灵活性。
因为 Windows 脚本文件不是
特定于引擎,它们可以包含
来自任何 Windows 脚本的脚本
兼容的脚本引擎。 他们行动
作为容器。

您可以使用与 .js 和 .vbs 文件相同的语法来调用该文件:

wscript.exe myScriptFile.wsf

更新:已注明更正...

You can achieve your goal by using the Windows Scripting Host instead of a specific .js or .vbs file. More information can be found at the MSDN site.

From the link:

Windows script (*.wsf) file is a text
document containing Extensible Markup
Language (XML) code. It incorporates
several features that offer you
increased scripting flexibility.
Because Windows script files are not
engine-specific, they can contain
script from any Windows Script
compatible scripting engine. They act
as a container.

You would cal lthe file using the same syntax as a .js and .vbs file:

wscript.exe myScriptFile.wsf

UPDATED: Correction noted...

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