Javascript object dependencies

Posted by Anurag on Stack Overflow See other posts from Stack Overflow or by Anurag
Published on 2010-03-07T00:44:17Z Indexed on 2010/03/08 10:21 UTC
Read the original article Hit count: 506

Filed under:
|
|

In complex client side projects, the number of Javascript files can get very large. However, for performance reasons it's good to concatenate these files, and compress the resulting file for sending over the wire. I am having problems in concatenating these as the dependencies are included after they are needed in some cases.

For instance, there are 2 files:

/modules/Module.js <requires Core.js>
/modules/core/Core.js

The directories are recursively traversed, and Module.js gets included before Core.js, which causes errors. This is just a simple example where dependencies could span across directories, and there could be other complex cases. There are no circular dependencies though.

The Javascript structure I follow is similar to Java packages, where each file defines a single Object (I'm using MooTools, but that's irrelevant). The structure of each javascript file and the dependencies is always consistent:

Module.js

var Module = new Class({
    Implements: Core,

    ...
});

Core.js

var Core = new Class({
    ...
});

What practices do you usually follow to handle dependencies in projects where the number of Javascript files is huge, and there are inter-file dependencies?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about files