Can i break up a js into multiple files?

Mar 18, 2010 at 2:49am

Can i break up a js into multiple files?

My js is getting unmanageably long…

#49187
Mar 18, 2010 at 6:38pm

Yes, but you might not want to.

There are 2 ways to do this that I’m aware of:

1. Put your code (or some of it) in your jsextensions folder.

2. Use File + eval:

/* seriously, don't use this unless you really know what's in the file. */
jsInclude.local = 1;
function jsInclude(filename) {
    var f = new File(filename);
    eval(readWholeFile(f));
}

readWholeFile.local = 1;
function readWholeFile(f) {
    var BLOCK_SIZE = 1024;
    if(f.isopen) {
	var s = "";
	var l;
	while(l = f.readstring(1024)) {
	    s += l;
	}
	return s;
    }
}
#176701
Mar 19, 2010 at 4:08pm

yeah, both of those seem like last resorts.

ok. other than ‘too much scrolling,’ is there a drawback to a long script? i thought there was a length limit, but i seem to have been mistaken.

Thank you

#176702
Mar 19, 2010 at 5:22pm

Hello maxer,

i have a 3000 lines javscript who works fine ; perhaps instantiation is a bit long, but i’m not sure, as the max patch is huge too :-)

#176703
Mar 20, 2010 at 2:04am

ok, well, it looks like that is the path i will take.

#176704
Mar 20, 2010 at 6:46am

Hello maxer,

with a good IDE (Eclipse, Xcode …) and autowatch = 1 ; that’s not so painful …

#176705
Mar 20, 2010 at 1:23pm

i am fond of xCode. pragma marks are my friend. but editing js in it seems sclunky compared to the smooth loving it gives me with obj C and the likes

Thank you though!

#176706
Mar 21, 2010 at 4:31am

I should mention, there is a third way, which is to use standard #include directives and then run your code through the C preprocessor. In other words, write your code in separate files, and use #include "myothercode.js" where you want to refer to another file. Then run the central file through the preprocessor to produce the file you actually point max at.

#176707
Mar 21, 2010 at 11:11am

that makes wonderful sense and i think i will do that.

#176708

You must be logged in to reply to this topic.