Learn from the bigger OO languages, use namespacing in code and folder structure.
For example, suppose you have the following folder structure:
You would define in each file the necessary namespaces:
if (!com) com = {};
if (!com.example) com.example = {};
if (!com.example.util) com.example.util = {};
com.example.util.Text = {
rot13: function(text) { ... }
}
To use, either use the full notation: com.example.util.Text.rot13, or define a temporary shortcut (var utilText = com.example.util.Text), somewhat like how you would do an "import" in other languages.
Namespacing helps you organize your code because you have to think about where things belong. It also helps you find code again when you need it. The cherry on top is that collisions with third party code are reduced to zero (provided you choose your top-level namespace correctly).
For actually integrating this into pages, use a combination of the following approaches:
- Build scripts for packaging up files
Concatenate the various files that go together (e.g. util.js contains all the files in the util folder)
- On-demand loader classes
Loading on-demand with a simple bootstrap class requires some thought on how you organize code, but it's worthwhile to be able to split the payload. I've gotten used to automatically designing my code so it can initialize itself after a loader callback, so writing code for on-demand loading has become second nature.
By the way, I shouldn't need to say this, but I will anyway: put all code into separate js files, even if it's specific to a page. It makes organizing your code much easier. If necessary, you can always use build scripts to combine files again for performance.