如何在单独的文件中创建 javascript 库并“包含” 它在另一个?
首先,需要注意的是。 主脚本不在网页中运行。 我将使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
此处的页面是我能找到的最接近的页面。 它讨论了 .wsf 文件,它允许您将多个脚本(可能用不同语言编写)合并到一个文件中。
对于你的问题,它应该是这样的:
user.wsf
可能有更好的方法,但我不是 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
There may be better ways, but I'm not a WSH expert.
我知道这是一个老问题,并且已经得到回答和接受。
我了解 .WSF 文件及其工作原理; 他们服务于目的。
然而,我还发现在 WSH 中运行的纯 .js 文件中进行“包含”很方便。 这是我的解决方案。
定义了该实用函数后,我可以在 javascript 模块内执行此操作:
...然后我可以调用任何函数或触发这些模块中定义的任何变量。
类似的技术适用于 .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.
With that utility function defined, I can do this from within a javascript module:
...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)?
您可以通过使用 Windows 脚本宿主而不是特定的 .js 或 .vbs 文件来实现您的目标。 有关详细信息,请访问MSDN 站点 。
从链接:
您可以使用与 .js 和 .vbs 文件相同的语法来调用该文件:
更新:已注明更正...
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:
You would cal lthe file using the same syntax as a .js and .vbs file:
UPDATED: Correction noted...