JavaScript/Notes/Singleton

From Noisebridge
< JavaScript(Difference between revisions)
Jump to: navigation, search
(Created page with "Singleton with information hiding. == Factory Method == function getAnObject(a) { var anObject; return (getAnObject = function() { if(! anObject ) { anObject =...")
 
(Factory Method)
Line 2: Line 2:
  
 
== Factory Method ==
 
== Factory Method ==
 +
<source lang="javascript">
 
function getAnObject(a) {
 
function getAnObject(a) {
 
   var anObject;
 
   var anObject;
Line 12: Line 13:
 
   })();
 
   })();
 
}
 
}
 +
</source>
  
 
== Instant Loaded ==  
 
== Instant Loaded ==  

Revision as of 11:53, 20 November 2013

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