JavaScript Design Patterns

The Factory Pattern

The factory pattern is a well - known design pattern used in software engineering to abstract away the process of creating specific objects. With no way to define classes in ECMAScript, developers created functions to encapsulate the creation of objects with specific interfaces - See more example at:

The Constructor Pattern

Constructors in ECMAScript are used to create specific types of objects. There are native constructors, such as Object and Array , which are available automatically in the execution environment at runtime. It is also possible to define custom constructors that define properties and methods for your own type of object. - See more with example at:

The Prototype Pattern

Each function is created with a prototype property, which is an object containing properties and methods that should be available to instances of a particular reference type. This object is literally a prototype for the object to be created once the constructor is called. The benefit of using the prototype is that all of its properties and methods are shared among object instances. Instead of assigning object information in the constructor, they can be assigned directly to the prototype - See more with example at:

Combination Constructor/Prototype Pattern

The most common way of defining custom types is to combine the constructor and prototype patterns. The constructor pattern defines instance properties, whereas the prototype pattern defines methods and shared properties. With this approach, each instance ends up with its own copy of the instance properties, but they all share references to methods, conserving memory. This pattern allows arguments to be passed into the constructor as well, effectively combining the best parts of each pattern - See more with example at:

Dynamic Prototype Pattern

Developers coming from other OO languages may find the visual separation between the constructor and prototype confusing. The dynamic prototype pattern seeks to solve this problem by encapsulating all of the information within the constructor while maintaining the benefits of using both a constructor and a prototype by initializing the prototype inside the constructor, but only if it is needed. You can determine if the prototype needs to be initialized by checking for the existence of a method that should be available - See more with example at:

Parasitic Constructor Pattern

The parasitic constructor pattern is typically a fallback when the other patterns fail. The basic idea of this pattern is to create a constructor that simply wraps the creation and return of another object while looking like a typical constructor - See more with example at:

Durable Constructor Pattern

    Douglas Crockford coined the term durable objects in JavaScript to refer to objects that have no public properties and whose methods don't reference the this object. Durable objects are best used in secure environments or to protect data from the rest of the application. A durable constructor is a constructor that follows a pattern similar to the parasitic constructor pattern, with two differences: instance methods on the created object don't refer to this, and the constructor is never called using the new operator - See more with example at:

Prototype Chaining

The basic idea is to use the concept of prototypes to inherit properties and methods between two reference types. Recall the relationship between constructors, prototypes, and instances: each constructor has a prototype object that points back to the constructor, and instances have an internal pointer to the prototype. What if the prototype were actually an instance of another type? That would mean the prototype itself would have a pointer to a different prototype that, in turn, would have a pointer to another constructor. If that prototype were also an instance of another type, then the pattern would continue, forming a chain between instances and prototypes. This is the basic idea behind prototype chaining. - See more with example at:

Constructor Stealing

    In an attempt to solve the inheritance problem with reference values on prototypes, developers began using a technique called constructor stealing (also sometimes called object masquerading or classical inheritance). The basic idea is quite simple: call the supertype constructor from within the subtype constructor. Keeping in mind that functions are simply objects that execute code in a particular context, the apply() and call() methods can be used to execute a constructor on the newly created object - See more with example at:

Passing Arguments

    One advantage that constructor stealing offers over prototype chaining is the ability to pass arguments into the supertype constructor from within the subtype constructor - See more with example at:

Combination Inheritance

    Combination inheritance (sometimes also called pseudoclassical inheritance) combines prototype chaining and constructor stealing to get the best of each approach. The basic idea is to use prototype chaining to inherit properties and methods on the prototype, and to use constructor stealing to inherit instance properties. This allows function reuse by defining methods on the prototype and allows each instance to have its own properties. - See more with example at:

Prototypal Inheritance

    In 2006, Douglas Crockford wrote an article entitled " Prototypal Inheritance in JavaScript " in which he introduced a method of inheritance that didn't involve the use of strictly defined constructors. His premise was that prototypes allow you to create new objects based on existing objects without the need for defining custom types. - See more with example at:

Parasitic Inheritance

    Closely related to prototypal inheritance is the concept of parasitic inheritance, another pattern popularized by Crockford. The idea behind parasitic inheritance is similar to that of the parasitic constructor and factory patterns: create a function that does the inheritance, augments the object in some way, and then returns the object as if it did all the work. - See more with example at:

Parasitic Combination Inheritance

    Combination inheritance is the most often - used pattern for inheritance in JavaScript, though it is not without its inefficiencies. The most inefficient part of the pattern is that the supertype constructor is always called twice: once to create the subtype's prototype, and once inside the subtype constructor. Essentially, the subtype property ends up with all of the instance properties of a supertype object, only to have it overwritten when the subtype constructor executes - See more with example at:

Whats next