Question: I am currently maintaining a large number of JS files and the dependency issue is growing over my head. Right now I have each function in a separate file and I manually maintain a database to work out the dependencies between functions.

This I would like to automate. For instance if I have the function f

```
Array.prototype.f = function() {};

```

which is referenced in another function g

```
MyObject.g = function() {
    var a = new Array();
    a.f();
};

```

I want to be able to detect that g is referencing f.

How do I go about this? Where do I start? Do I need to actually write a compiler or can I tweak Spidermonkey for instance? Did anyone else already do this?

Any pointers to get me started is very much appreciated

Thanks
Dok

Answer: 
Have you tried using a dependency manager like [RequireJS](http://requirejs.org/) or [LabJS](http://labjs.com/)? I noticed no one's mentioned them in this thread.

From <http://requirejs.org/docs/start.html>:

> 
> Inside of main.js, you can use require() to load any other scripts you
>  need to run:
> 
> 
> 

```
require(["helper/util"], function(util) {
    //This function is called when scripts/helper/util.js is loaded.
    //If util.js calls define(), then this function is not fired until
    //util's dependencies have loaded, and the util argument will hold
    //the module value for "helper/util".
});

```

You can nest those dependencies as well, so helper/util can require some other files within itself.
I have written a tool to do something like this: <http://github.com/damonsmith/js-class-loader>

It's most useful if you have a java webapp and you structure your JS code in the java style. If you do that, it can detect all of your code dependencies and bundle them up, with support for both runtime and parse-time dependencies.