JavaScript/Notes/Singleton

From Noisebridge
< JavaScript
Revision as of 11:52, 20 November 2013 by Garrett (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Singleton with information hiding.

Factory Method

function getAnObject(a) {

 var anObject;
 return (getAnObject = function() {
   if(! anObject ) {
     anObject = {name: a};
   }
   return anObject;
 })();

}

Instant Loaded

var anObject = new function(a) {

 this.name = a;

};

Personal tools