JavaScript Singleton Patterns

The JavaScript Singleton Design Pattern seeks to ensure the singleton object can only be created once.

To this end, it is my assertion the best and simplest approach is to simply use a global variable because global variable instances can only exist once and only once and by definition a singleton is a static entity and static is just another way to say global.

As you can see from the links below, there are seemingly numerous ways to get to something one might be tempted to call a Singleton however they all have one thing in common, they all make use of a static to anchor the Singleton into memory… because a Singleton is a static, clean and simple.

The bottom line is no matter how long a way you want to go to get to a Singleton in JavaScript there is only one way to make a Singleton remain in memory and that way is to use a global variable; the rest is nothing more than window-dressing. Those who favor the window-dressing over what sits behind the window are the very people who wish to appear to be smart and intelligent without so much as actually being smart and intelligent. Those who are smart and intelligent will look for methods for doing their jobs that allow them to get more done in less time since time is the finite resource that one can never replace. You will only get a very short limited time with which to impress those who may engage you to get their work done so why not get as much done as possible in a short a time as possible – doing so will only serve to impress their socks off so much so they will likely seek to give you more money rather than not especially if you can continue to impress them every single time they ask you to do some work for them. Far too many professionals seem to think they should do their work for their clients in a manner than consumes as much time as possible by making their design patterns as flowery and ornate as possible while at the same time making others have to look and study what they have done since at the core of what they have done is the simple concept that says a Singleton is always a static variable and nothing more.

Consider this Singleton Pattern: (re: Javascript Design Patterns – 1. The Singleton)

The author wishes to make the Singleton be a single instance thing by denying the ability to make any more instances because… the Class is destroyed the moment the Singleton is created. This is exactly what a global variable is, a single instance that cannot be replicated. (see more below)

Consider this Simpler Singleton Pattern: (re: The Agile Developer’s Blog)

This code performs exactly the same functions as that of the previous example without the need to use prototype.js.

Why does this work ?

After-all, at the end of the day, for JavaScript an Object is just an object regardless of how one gets there.

There is no way to make another instance of mySingleton from the object template since it is assigned to a global and there is no Class from which to make another instance.

One can still make use of a closure, if desired to hide the inner-workings in a nice safe and secure spot, well almost nice and safe until this product come alive.

BTW – mySingleton is a name-space within which one can place items that are segregated from the rest of the global objects, just as-if a Closure had been used.

References:

Singleton Design Pattern

Javascript: best Singleton pattern

WikiPedia’s version

Object-Oriented JavaScript Tip: Implementing The Singleton Pattern

Javascript Design Patterns – 1. The Singleton

The Agile Developer’s Blog

About Ray C Horn
See my profile at http://www.linkedin.com/in/raychorn with more than 1286+ connections and growing all the time.

Comments are closed.