2 ms·
Ask HN: How do you deal with Big JavaScript files?
Hi,
I'm building a JavaScript engine. I used a simple function as a class, added subfunctions, strings... and used prototypes to add other objects as needed...
Everything is fine. The problem now, is that I'm using one class (or main function), because I need it to be so. It's becoming huge, to the point that I can't control it any more and debugging the code is becoming as hard as hell.
I'm a C# developer, who used Visual Studio. I never come across this problem, because I have different files/classes/forms...
I wonder how developers here deal with large JavaScript files. What tools/strategies/techniques do you use to solve this issue.
Note: I want to use ONE main function (or javascript class).
Thanks
- mbrubeck 17y agoI don't understand. WHY do you want to use one main function or class? Why not break things up into modules, classes, and functions similar to the way you do in C#? Look at the source code of any well-written JavaScript library or app and you will find quite a bit of modularity. Some examples, from small to large: http://osteele.com/sources/javascript/functional/functional.js http://osteele.com/sources/javascript/functional/functional.... http://github.com/jdalton/fusejs/tree/master/src/ http://github.com/jdalton/fusejs/tree/master/src/ http://github.com/aptana/activejs/tree/master/src/active_record/ http://github.com/aptana/activejs/tree/master/src/active_rec... It makes sense to use one global object as a namespace, but within this namespace you can have as many classes, objects, and functions as you like. Look at the source of jQuery, for example: http://code.jquery.com/jquery-1.4.1.js http://code.jquery.com/jquery-1.4.1.js Even if you have a single class, you can define that classes' members across many different files. You can concatenate all the files together at build- or deployment-time (for performance reasons) but keep them separate in your source tree. For example, the jQuery source code I linked above is actually a concatenation of half a dozen different source files. Just use the "module pattern" to wrap each file in an anonymous function, and have it export its public interfaces to objects/classes in your single global namespace: http://www.yuiblog.com/blog/2007/06/12/module-pattern/ http://www.yuiblog.com/blog/2007/06/12/module-pattern/