如何在单独的文件中创建javascript库并“包含”它在另一个? [英] How can I create a javascript library in a separate file and "include" it in another?

查看:128
本文介绍了如何在单独的文件中创建javascript库并“包含”它在另一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,一个警告。主要脚本未在网页中运行。我将使用Windows脚本宿主在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.

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

这篇关于如何在单独的文件中创建javascript库并“包含”它在另一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆