How can I create a javascript library in a separate file and "include" it in another?

user38735 picture user38735 · May 29, 2009 · Viewed 9.2k times · Source

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)

Answer

Ionuț G. Stan picture Ionuț G. Stan · May 29, 2009

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>

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