DesignPatterns solve many recursive problems that are encountered in an elegant way. However, most are defined for class-based languages. Since JavaScript is a PrototypeBasedLanguage, we can face problems in implementing many of these patterns. But with JavaScriptCodingStandard and some fixing of code, we can overcome this problem. Note that there is a more efficient way to do these patterns. The optimal implementation is left as an exercise for the reader. Some suggestions are: '''SingletonPattern''' I think this is one of the most used (and misused) patterns. The following code tries to use SingletonPattern in JavaScript. // Handles events for the GUI framework. Event''''''Handler.instance_ = new Event''''''Handler(); Event''''''Handler.getInstance = Event''''''Handler_getInstance; // Constructor. function Event''''''Handler() { if(Event''''''Handler.instance_!=null) alert("Cannot make multiple instances!"); } // Returns singleton instance. function Event''''''Handler_getInstance() { return Event''''''Handler.instance_; } You can also use closure to make a singleton. Here is an example: var ASingleton = function() { var AConstructor = function AConstructor() { this.name = "John Criton"; this.occupation = "Pain in the Butt"; }; var instance = new AConstructor(); return function () {return instance;}; }(); // Make some singletons // no need for getinstance functions var vv = new ASingleton(); var ww = new ASingleton(); // true if same object, otherwise false window.alert(vv === ww); Here, the sole instance is constructed when declaring the function. You can also force it to construct the instance using LazyInitialization. The following can also be considered a simple Singleton. var aSingleton = { name: "John Criton", occupation: "Pain in the Butt", toString: function() { return this.name + " is a " + this.occupation; } } The version you choose for your project depends on its requirements, complexity, and your personal coding style. ''' AbstractFactory pattern''' AbstractFactory creates instances of another class. This adds flexibility in the code such that the calling class is not directly linked to the class whose instance it needs. In browser world this pattern is a boon! An example. // Factory of Gui''''''Component. Gui''''''Component''''''Factory.instance_ = new Gui''''''Component''''''Factory(); Gui''''''Component''''''Factory.getInstance = Gui''''''Component''''''Factory_getInstance; function Gui''''''Component''''''Factory() { if(Gui''''''Component''''''Factory.instance_!=null) alert("Cannot make multiple instances!"); this.createGuiComponent = Gui''''''Component''''''Factory_createGuiComponent; } function Gui''''''Component''''''Factory_getInstance() { return Gui''''''Component''''''Factory.instance_; } function Gui''''''Component''''''Factory_createGuiComponent() { if(Browser.getInstance().getType()==Browser.NETSCAPE) return Gui''''''Component''''''Factory_createNetscapeGui(); return Gui''''''Component''''''Factory_createNormalGui(); } function Gui''''''Component''''''Factory_createNetscapeGui() { return new Netscape''''''Gui''''''Component(); } function Gui''''''Component''''''Factory_createNormalGui() { return new Gui''''''Component(); } '''PrototypePattern''' This is one of the easiest patterns for JavaScript, since it is built into the language: var APrototype = function() { this.name = "John Criton"; this.occupation = "Pain in the Butt"; this.toString = function() { return this.name; }; }; var AClient = function() { this.toString = function() { return this.occupation; }; }; AClient.prototype = new APrototype(); // Make a client var vv = new AClient(); window.alert(vv); While not strictly like the GangOfFour's intent, Prototypes can be used to implement the '''TemplateMethodPattern''' in JavaScript as well: var APrototype = function() { this.toString = function() { return this.step1() + this.step2() + this.step3(); }; }; var AClient = function() { this.step1 = function() {return "John Criton";}; this.step2 = function() {return " is ";}; this.step3 = function() {return "a pain in the butt!";}; }; AClient.prototype = new APrototype(); // Make a client var vv = new AClient(); window.alert(vv); '''DecoratorPattern''' You can also use the prototype property to decorate an object with new responsibilities without changing the base object: // Decorates a W3C DOM Level 1 document object var Decorated''''''Document = function(doc) { var Constructor = function() { this.popupTitle = function() { window.alert(this.title); }; }; Constructor.prototype = doc; return new Constructor(); }; // Make a client window.onload = function() { var vv = new Decorated''''''Document(window.document); vv.popupTitle(); }; ---- '''Tip''' The DocumentObjectModel contains many more examples of common DesignPatterns. ---- I'm curious why you would say JavaScript is a difficult language. I find it to be one of the most flexible and easy to work with and most powerful of just about any language. It supports autodelegation via PrototypeBased objects, DynamicTyping rather than StaticTyping, optional parameters in a standard argument list, full LexicalClosure''''''s - frankly, it's almost LispLanguage in CeeLanguage's clothing. If you like ObjectOriented and FunctionalProgramming, you should absolutely love JavaScript: it's a wonderful little language. ''As you've observed, Lisp provides a superset of the semantics of JavaScript. Lisp is supported, documented, mature, and has a sparse, expressive, and trivially-parsed syntax. JavaScript, conversely, buries much or all of Lisp's semantics beneath a baroque syntax and horribly antiquated tools. Far from being a "wonderful little language", I find it a cheap imitation. -- TomStambaugh'' It may be a cheap imitation of Lisp, but then again, what isn't? The fact is it's a language we can actually use in our day-to-day work and it has many of Lisp's niceties. Can't really complain about that. JavaScript is seen as CommonLisp with another syntax: http://lambda-the-ultimate.org/classic/message8778.html ---- CategoryJavaScript