Forums > Javascript

include statements possible?


August 27, 2006 | 1:27 pm

Is it possible to split js code over several files? I know you can add code
to the jsextensions folder, but changes there require a restart. My code is
making me dizzy because of its filelength, and therefor l like to divide it
over several files using simple include statements. I can’t find anything
about it.

include "foo.js"; is not accepted

-thijs

September 6, 2006 | 12:32 pm

Yes, can someone please answer this?
I’ve tried including a file with a class defined in it in the jsextensions directory, but when I try extending it in js, I always get a JavascriptReference Error: class XXXX not found.

Can someone else try saving this code to their jsextensions directory:

———————
function GUIHandler()
{
this.className = "GUIHandler";
}

GUIHandler.prototype.clicked = function(data,x,y,button,cmd,shift,capslock,option,ctrl)
{}
GUIHandler.prototype.dblclicked = function(data,x,y,button,cmd,shift,capslock,option,ctrl)
{}
——————–

and then try loading this code into a js or jsui object and tell me if it works for them?

———————–
function ExampleGUIHandler()
{
this.className = "SillyGUIHandler";
this.parent = GUIHandler;
}

ExampleGUIHandler.prototype = new GUIHandler;

ExampleGUIHandler.prototype.clicked = function(data,x,y,button,cmd,shift,capslock,option,ctrl)
{
outlet(0,"clicked", data,x,y,button,cmd,shift,capslock,option,ctrl);
clickedButton = data;
}
ExampleGUIHandler.prototype.dblclicked = function(data,x,y,button,cmd,shift,capslock,option,ctrl)
{
outlet(0,"dblclicked", data,x,y,button,cmd,shift,capslock,option,ctrl);
dblclickedButton = data;
}
——————————

September 6, 2006 | 12:55 pm

hi evan,

I still don’t know about include statements, I quess it’s nog possible, but
jsextensions work like this:

js file in extensions contains:

function EvilFoo(){
this.member = 666;
}

if the extensions are loaded you can simple use this in your js:

var foo = new EvilFoo();
post(foo.member); // this will return 666

I don’t know what you’re doing with the this.parent, but maybe you just
missed the () at the new statement??

hth, -thijs

On 9/6/06, evan.raskob

wrote:
>
>
> Yes, can someone please answer this?
> I’ve tried including a file with a class defined in it in the jsextensions
> directory, but when I try extending it in js, I always get a
> JavascriptReference Error: class XXXX not found.
>
> Can someone else try saving this code to their jsextensions directory:
>
> ———————
> function GUIHandler()
> {
> this.className = "GUIHandler";
> }
>
> GUIHandler.prototype.clicked =
> function(data,x,y,button,cmd,shift,capslock,option,ctrl)
> {}
> GUIHandler.prototype.dblclicked =
> function(data,x,y,button,cmd,shift,capslock,option,ctrl)
> {}
> ——————–
>
> and then try loading this code into a js or jsui object and tell me if it
> works for them?
>
> ———————–
> function ExampleGUIHandler()
> {
> this.className = "SillyGUIHandler";
> this.parent = GUIHandler;
> }
>
> ExampleGUIHandler.prototype = new GUIHandler;
>
> ExampleGUIHandler.prototype.clicked =
> function(data,x,y,button,cmd,shift,capslock,option,ctrl)
> {
> outlet(0,"clicked",
> data,x,y,button,cmd,shift,capslock,option,ctrl);
> clickedButton = data;
> }
> ExampleGUIHandler.prototype.dblclicked =
> function(data,x,y,button,cmd,shift,capslock,option,ctrl)
> {
> outlet(0,"dblclicked",
> data,x,y,button,cmd,shift,capslock,option,ctrl);
> dblclickedButton = data;
> }
> ——————————
>


sxa
September 27, 2007 | 11:21 am

I know this is a -very- old thread, but, I was looking for a means of using include files, and wound up writing a basic DIY version. It basically relies on eval(), which -is- possibly inefficient, but it sucessfully allows me to use included files in my javascript code.

The code needs to be pasted into one’s own js code, and supports
both ‘local’ and ‘global’ includes; the former live in the same directory as one’s own js file, the latter live in a single central repository (defaulting to "Cycling ’74jsincludes" as Im using Windows).
The syntax is derived from cpp, so
include("lib") is a local file and
include("

") is a global file
For ease of developing a library of includable routines, the include routine looks for a file suffixed ‘.h.js’ -then- a file suffixed ‘.js’. I use the former filenaming convention for ‘cleaned’ libraries stripped of comments and whitespace (so there’s less to read/eval), and the latter for ‘development’ libraries which might be heavily commented and indented.

example usage
————–
//initialise buffer
var includes = "";
// include various files
include("local");
include("");
include("");
// merge included code
eval(includes);
// reclaim string buffer
includes = "";

Hopefully its useful to someone. Ive used it to structure my code, as well as write ‘support’ libraries to do things like simplify inlet/outlet creation, and provide ‘max-identical’ attribute/argument declaration/handling.

-include.js code attached-

Viewing 4 posts - 1 through 4 (of 4 total)

Forums > Javascript