views:

344

answers:

4

I have been learning more and more javascript; it's a necessity at my job. We have a web application that uses a lot of javascript and I will be doing more and more every day. I have read bits and pieces about design patterns, but was wondering if someone could just give me a cut and dry example and definition. Are they something that would benefit me? Or is it more high level?

+7  A: 

Design patterns are generic and usually elegant solutions to well-known programming problems. Without knowing what problem you're working in, I would say "Yes" they can help make your code more manageable.

This link and this link make some reference to design patterns in Javascript. They may be worth reviewing.

Peter K.
+5  A: 

Also there is a book about classic design patterns in javascript. You can download examples from it's site.

But from my experience its obviuosly harder to implement projects with great amount of javascript.

maxnk
+5  A: 

One of the most practical and easy to use JavaScript-specific design pattern I've encountered is the Module Pattern, which is a modified Singleton pattern that "namespaces" related code and prevents the global scope from getting cluttered with variables and functions that might conflict with each other in a complicated page.

Kevin Gorski
+1  A: 

As design patterns are language agnostic, I would recommend reading one of the classic books on the subject. My favourites are:

However, these aren't beginner books by any means and you might get more value out of some of the many web resources and tutorials out there (Wikipedia has some reasonable explanations).

My own experience is that the object model in javascript is a bit trickier to understand than others such as PHP or Ruby and hence, applying design patterns isn't always that easy. Libraries such as Prototype provide functions for making inheritance easier to work with and this makes programming Javascript in an OO way much easier.

I recently used Javascript to implement the Active Record pattern using Prototype, which you can read about more about here if you want: http://codeinthehole.com/archives/6-Active-record-javascript-objects-using-cookies.html

DavidWinterbottom