The Javascript Singleton Pattern – Part I   Leave a comment

The Singleton Saga – Part I
The Singleton Saga – Part II
The Singleton Sage – Part III

This week’s featured photograph is from Flickr poster

just john.

Title: hotel couch

First Principles:  A JavaScript function is an object.  An object can contain other objects, both public and private.These inner objects can be variables or functions.  Before getting to the Singleton Pattern, I will need to first introduce the Module Pattern (discussed in this part) and the scary Closure Pattern (in part II) before getting to the actual Singleton Pattern (Part III)

So lets start with two simple Module Patterns.  Note:  through out this discussion we are going to use JavaScript Object Notation (JSON) by default most of the time to define objects.  If there is enough interest in this sub topic I will devote a post to this syntax as well.

The Module Pattern:

 var myPublicModule={
 MyPublicObject : 'a string value',
 MyPublicFunction: function(stringParameter){
 return stringParmater.toUpperCase();

Note the comma which is used to separate JSON defined objects within the module.



This pattern does not do much except:

  • Reduce the number of global variables
  • Bring common functions and variables into a common namespace.

For more on Namespaces see: Create Advanced Web Applications With Object-Oriented Techniques

What’s the downside? Once we start defining things with JSON, in this pattern, we can not add private
functions or variables to the module.  We can add private objects (and what Crockford calls Previlaged Objects) by invoking Closures within the Module – but that’s a topic for a later post.

The Singleton Saga – Part I
The Singleton Saga – Part II
The Singleton Sage – Part III

Posted 2009/06/08 by Cloud2013 in Javascript, Photography, Singleton

Tagged with , , ,

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: