23 ms·
I'll assume this is for client side js, since that's what most people use =) Most of the js I write involves jQuery. As such, I find it's very useful to separa
by csytan 17y ago
I'll assume this is for client side js, since that's what most people use =)
Most of the js I write involves jQuery. As such, I find it's very useful to separate reusable components into jQuery plugins in separate files. If you don't use jQuery or another library which allows plugins, then it's best to create your own namespaced module.
See the "Module pattern" for more info:
http://www.yuiblog.com/blog/2007/06/12/module-pattern/ http://www.yuiblog.com/blog/2007/06/12/module-pattern/
Depending on the complexity of your code, you might want to look into javascript build systems -- there's at least one for every major web framework.
Here's some advantages they offer:
- Allow bunding of many js files into one mega js file. (reduces HTTP requests for faster page loading)
- Specify javascript dependencies using special syntax. (easier maintenance)
- Versioning support (so users don't get an old cached version of your code)
When bundling js files, I try to aim for a maximum of two js files per page. One contains code which is global to the whole site (this is cached on subsequent views), and the second which contains page specific code.