最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Are design patterns in JavaScript helpful? And what exactly are they? - Stack Overflow

programmeradmin1浏览0评论

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?

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?

Share Improve this question edited Dec 14, 2008 at 15:32 Greg 322k54 gold badges376 silver badges337 bronze badges asked Dec 14, 2008 at 15:27 jrutterjrutter 3,21310 gold badges45 silver badges51 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 11

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.

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.

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.

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

  • Patterns of Enterprise Application Architecture by Martin Fowler (http://www.amazon.com/Enterprise-Application-Architecture-Addison-Wesley-Signature/dp/0321127420)
  • Design Patterns: Elements of Reusable Object-Oriented Software by the "Gang of Four" (http://www.amazon.com/Design-Patterns-Object-Oriented-Addison-Wesley-Professional/dp/0201633612/ref=sr_1_1?ie=UTF8&s=books&qid=1229278937&sr=1-1). This is a bit of a classic.

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

发布评论

评论列表(0)

  1. 暂无评论