JavaScript/Notes/Singleton

From Noisebridge
< JavaScript(Difference between revisions)
Jump to: navigation, search
(Instant Loaded)
(Eager Initialization)
Line 18: Line 18:
 
<source lang="javascript">
 
<source lang="javascript">
 
var anObject = new function(a) {
 
var anObject = new function(a) {
   this.name = a;
+
  var b = a + 2;
 +
   this.name = b;
 
};
 
};
 
</source>
 
</source>

Revision as of 11:55, 20 November 2013

Singleton with information hiding.

Factory Method

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

Eager Initialization

var anObject = new function(a) {
  var b = a + 2;
  this.name = b;
};
Personal tools