views:

495

answers:

4

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)

A: 

Maybe this phpjs implementation could help.

http://phpjs.org/functions/include:433

Renato Aquino
That solution is targeted at browsers.
Ben Blank
+9  A: 

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.

Ionuț G. Stan
Geesh... Beat me to the answer.... ;-)
Dscoduc
A: 

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:

Windows script (*.wsf) file is a text document containing Extensible Markup Language (XML) code. 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. They act as a container.

You would cal lthe file using the same syntax as a .js and .vbs file:

wscript.exe myScriptFile.wsf

UPDATED: Correction noted...

Dscoduc
The script host is either cscript.exe or wscript.exe, not vbscript.exe.
Helen
A: 

Sprockets is a Javascript dependency management library.

http://getsprockets.org/

Is uses a Ruby script to create a single Javascript source file from any number of Javascript sources files. It manages dependencies. It is documentation friendly.

Alan Gutierrez