[英]How can I create a javascript library in a separate file and “include” it in another?
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. 问题:我想创建一个包含许多对象的javascript“库”,每个对象都有许多功能。 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 ).
我希望这个库会随着时间的推移变得相当大,并希望将它保存在一个单独的javascript文件中(我们称之为Library.js )。 I would like to access the objects from Library.js from another script (let's call this one User.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? 反正有没有在javascript中实现这个? (Remember, this is not web-based)
(请记住,这不是基于网络的)
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 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专家。
I know this is an old question and it has been answered and accepted. 我知道这是一个古老的问题,已得到回答和接受。
I know about .WSF files and how they work; 我知道.WSF文件及其工作原理; 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. 但是,我也发现在WSH中运行的纯.js文件中执行“包含”很方便。 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: 定义了该实用程序功能后,我可以在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: 类似的技术适用于.vbs文件:
How do I include a common file in VBScript (similar to C #include)? 如何在VBScript中包含公共文件(类似于C #include)?
You can achieve your goal by using the Windows Scripting Host instead of a specific .js or .vbs file. 您可以使用Windows Scripting Host而不是特定的.js或.vbs文件来实现目标。 More information can be found at the MSDN site .
可以在MSDN站点上找到更多信息。
From the link: 从链接:
Windows script (*.wsf) file is a text document containing Extensible Markup Language (XML) code.
Windows脚本(* .wsf)文件是包含可扩展标记语言(XML)代码的文本文档。 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.
由于Windows脚本文件不是特定于引擎的,因此它们可以包含来自任何Windows脚本兼容脚本引擎的脚本。 They act as a container.
它们充当容器。
You would cal lthe file using the same syntax as a .js and .vbs file: 您可以使用与.js和.vbs文件相同的语法来编写文件:
wscript.exe myScriptFile.wsf
wscript.exe myScriptFile.wsf
UPDATED: Correction noted... 更新:更正指出......
Maybe this phpjs implementation could help. 也许这个phpjs实现可以帮助。
http://phpjs.org/functions/include:433 http://phpjs.org/functions/include:433
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.