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

时间:2021-11-01 19:44:32

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.

首先,一个警告。主脚本不在网页中运行。我将使用Windows脚本宿主在Windows中运行.js文件。

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).

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

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

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

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

反正有没有在javascript中实现这个? (请记住,这不是基于网络的)

4 个解决方案

#1


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.

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

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.

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

#2


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.

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

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.

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

    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:

通过定义该实用程序功能,我可以在javascript模块中执行此操作:

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)?

类似的技术适用于.vbs文件:如何在VBScript中包含公共文件(类似于C #include)?

#3


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.

您可以使用Windows Scripting Host而不是特定的.js或.vbs文件来实现目标。可以在MSDN站点上找到更多信息。

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.

Windows脚本(* .wsf)文件是包含可扩展标记语言(XML)代码的文本文档。它结合了多种功能,为您提供更高的脚本编写灵活性。由于Windows脚本文件不是特定于引擎的,因此它们可以包含来自任何Windows脚本兼容脚本引擎的脚本。它们充当容器。

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

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

wscript.exe myScriptFile.wsf

UPDATED: Correction noted...

更新:更正说明......

#4


Maybe this phpjs implementation could help.

也许这个phpjs实现可以帮助。

http://phpjs.org/functions/include:433

#1


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.

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

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.

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

#2


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.

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

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.

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

    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:

通过定义该实用程序功能,我可以在javascript模块中执行此操作:

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)?

类似的技术适用于.vbs文件:如何在VBScript中包含公共文件(类似于C #include)?

#3


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.

您可以使用Windows Scripting Host而不是特定的.js或.vbs文件来实现目标。可以在MSDN站点上找到更多信息。

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.

Windows脚本(* .wsf)文件是包含可扩展标记语言(XML)代码的文本文档。它结合了多种功能,为您提供更高的脚本编写灵活性。由于Windows脚本文件不是特定于引擎的,因此它们可以包含来自任何Windows脚本兼容脚本引擎的脚本。它们充当容器。

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

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

wscript.exe myScriptFile.wsf

UPDATED: Correction noted...

更新:更正说明......

#4


Maybe this phpjs implementation could help.

也许这个phpjs实现可以帮助。

http://phpjs.org/functions/include:433