w3schools - What Are Some Examples of Design Pattern Implementations Using JavaScript?

javascript design patterns w3schools (4)

I just wanted to add my favorite JavaScript pattern that I learned from Pro JavaScript Design Patterns which Jonathan Rauch already recommended.

It is the namespace singleton pattern. Basically, you create namespaces via singletons which allow you to hide methods and variables from external use. The hidden/exposed methods are actually hidden because they are defined within the closure.

var com = window.com || {};
com.mynamespace = com.mynamespace || {};
com.mynamespace.newpackage = (function() {
    var myPrivateVariable = "hidden";
    var myPublicVariable = "exposed";

    function myPrivateMethod() {
        return "also hidden";

    function myPublicMethod() {
        return "also exposed";

    return {
        myPublicMethod: myPublicMethod,
        myPublicVariable: myPublicVariable

I'm a moderately skilled programmer using JavaScript but I am no guru. I know you can do some pretty powerful things with it, I just haven't seen much other than fairly basic DOM manipulation. I'm wondering if people could provide some examples of traditional design pattern concepts such as Factory Method, Singleton, etc using JavaScript. In what cases would these patterns be used for on the web?

Nickolay touched on this, but design patterns are not consistent between languages that have drastic differences. I have read before (and agree with) that a design pattern is often a sign of missing features in the language.

As a perfect example, the "Factory" pattern is completely unnecessary in some languages, Ruby as the example I am thinking of (because object construction is just a method on the Class instance):

# create a factory for MyObject
my_factory = MyObject
instance_1 = my_factory.new

# change the factory to another class
my_factory = MyOtherObject
instance_2 = my_factory.new

Whenever you are applying a design pattern to a different language than it was initially developed for, make sure you consider if it is REALLY necessary, and what ways you might be able to improve it with the new language features available. Design patterns are just a guide, you should always consider if the use you intend really needs the pattern, or if it can be adopted in a better way in your case.

To clarify a bit for the comments below, most of the time it's creating a closure, it keeps your variables scoped to that local closure, as to not create global variables, it both keeps things clean and avoids any potential unwanted changes to those variables.

There are some excellent answers here that explain the why a bit more: How does a javascript closure work?

It's only a creating closure when something inside that scope is exposed to an outer scope, which is usually the case, but I can't be sure for your example without seeing more code. If nothing is exposed then no closure's created...otherwise it's just an anonymous function executing immediately.

The })(); format at the end, as opposed to }); is actually calling that closure to execute immediately, with no parameters. If you had something in it, for example })(something); then that something would be passed as the first argument here: (function(somethingParam){.