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

2022-01-25 00:00:00 include javascript wsh windows-scripting

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

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.

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

本质上,我正在寻找类似于 C/C++包含"范式的东西.

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

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

推荐答案

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

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>

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

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

相关文章