A lot of web frameworks have a MVC-style layout to code and approaching problems. What are some good similar paradigms for JavaScript? I'm already using a framework (jQuery) and unobtrusive js, but that still doesn't address the problem I run into when I have more complex web apps that require a lot of javascript, I tend to just end up with a bunch of functions in a single, or possibly a few files. What's a better way to approach this?
|
Namespacing: Don't forget to use objects as a pseudo-package (most frameworks do this, you should also):
You don't necessarily need to set it up that way, and use whatever naming conventions you prefer, but the namespacing will help you out a lot (makes it easier on refactoring search-and-replace too, when you decide to move something). |
|||||
|
some people don't realize that JS is fully object oriented. You can create something that works just like a class:
you can instantiate those classes:
and it supports inheritance:
and can even add methods to existing classes:
If you'd like to learn more about JavaScript's OO support, I highly recommend the Rhino book. |
|||||||
|
Using jQuery is a great start. JavaScript in a browser augments the frontend, there isn't a MVC "pattern" or anything to follow. There are some "best practices" that help keep the code clean.
For server side / non-browser JavaScript (like Node.js) there are several great frameworks for building off of. It might be worth while browsing through the source to see if you pick up on any thing. I moved this to a more in-depth entry for the interested. |
|||||||
|
This has been my experience: It's very difficult to 'design' JS stuff. The best I've been able to do is group like functions together in the same file, and only include that file where necessary. I try to follow standard coding conventions, etc when coding, but JS is so fluid that it's not always possible, especially when trying to generically manipulate page elements. I'd say just do the best you can to organize. Here is an example of using namespaces is JavaScript that may help you out. It makes it so your code requires only one global variable, cleaning up the global space a good bit. |
|||
|
I'd recommend using namespacing and then a file structure that mirrors the namespace. For example, using Brad's answer, I'd have With an organised codebase of prototypal objects you should be in a position to implement most design patterns. There are also some good books on this subject. This presentation by Nicholas Zackas is also useful to get some insight in how to build good Javascript apps. Also, in Javascript, I think its also important to distinguish between patterns and techniques. You need to be familiar with various commonly used techniques which JS natively lacks, such as namespacing. But these are techniques not patterns; techniques which make it easier to build patterns such as those defined by the Gang of Four. Sorry this such a brief summary. But I'd strongly recommend the resources I mentioned to get greater insight. |
|||
|