Javascript singleton - to function or to json, that is the question

Forums » Generic » Java Script

You need to be logged in to post in the forums. If you do not have an account, please sign up first.

Go to last post

2. May 2010, 11:14:15

OmegaJunior

Posts: 334

Javascript singleton - to function or to json, that is the question

We can add singletons in javascript using various methods. I want to know which is best for what purpose, and why.

JSON method:

The singleton is an object with methods and properties.

Example:

var myNamespace=({
 message:'hello, world!'
 ,
 greeting:function(){
  alert(myNamespace.message)
 }
})


This method I've been using for years.


Function method:

The singleton is an anonymous function with methods and properties.

Example:

var myNamespace=function(){
 var message='hello, world!'
 ;
 var greeting=function(){
  alert(message)
 }
 return({
  message:message,
  greeting:greeting
 })
}();


This method I see in samples of other people's code (Google Analytics and Maps, for instance).


Similarities
  1. Both methods result in a singleton
  2. Both result in accessible properties and methods
  3. Both methods help prevent cluttering the global namespace


Differences
  1. The function method requires execution of its script, resulting in a longer loading time (which we can detect using the Chromium extension 'Speed Tracer')
  2. The function method allows for naming variations between private and public members
  3. The members of the function singleton recognise each other, whereas those of the json singleton require the namespace prefix (therefore one could argue that the members of the json singleton aren't really its members which leads to the conclusion that the json method doesn't lead to a real singleton)
    [/list=1]

    Thoughts?

Singleton - to function or to json?

[ Please login to vote ]

view results

The fly assumes the bear cares about its antics...

12. May 2010, 14:16:58

Opera Software

hallvors

Opera Software

Posts: 1778

Originally posted by OmegaJunior:

The function method allows for naming variations between private and public members


Originally posted by OmegaJunior:

The members of the function singleton recognise each other,



I think these are the main reasons for choosing to use a (function(){})() wrapper.

Where code is as simple as in your chosen examples, it doesn't many any difference what syntax you use. However, when things get more complex, the object literal syntax - { 'name':function(){}, 'name2': function(){}) - becomes harder to use. We're used to terminating statements with semicolons, maintaining a whole file of code where certain statements are separated with a comma and keeping the punctuation correct is harder.
--
My blog: miscoded
Stupid code from major websites uncovered and criticised
Contribute site fixes! - OTW&TA- all sites must work

13. May 2010, 21:34:34

OmegaJunior

Posts: 334

Thank you for your feedback, Hallvord!

On punctuation: I see your point, and I disagree. I've written singletons using a JSON wrapper for several years, so from experience I can tell that yes, it is more difficult than say, defining separate functions and variables in the global namespace, but one also gets used to it. And once we do get used to it, I doubt that a punctuation difference will throw us off.

I was trying to compare size difference to compilation speed... does it hurt the page's performance more that it needs to download several extra kilobytes (in case of a JSON wrapper), or that it needs to evaluate one huge function (in case of a Function wrapper)? But I guess the conclusion would differ depending on the client machine and network connection.

One other difference I found: using a JSON wrapper, the resulting object still compiles, when a few of its methods turn up faulty. Using a Function wrapper, the resulting object will refuse to compile entirely, when a few of its methods turn up faulty. Again, we could use this diffence to show that a JSON-wrapped singleton isn't a real singleton at all. It does allow us a few degrees of freedom, which can be both usefull when debugging, as well as annoying because a single faulty function won't block compilation and thus may slip under the radar.

Thus, in favour of the Function-wrapped singleton:
* member naming aliases
* member protection / hiding
* members recognise each other
* a faulty member blocks compilation
* uses regular punctuation

Other considerations?
* Memory footprint - which wrapper consumes more memory?
The fly assumes the bear cares about its antics...

Forums » Generic » Java Script