JavaScript/Notes/Singleton: Difference between revisions
No edit summary |
|||
(9 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
Singleton with information hiding. | Singleton with information hiding. | ||
== Constructors == | |||
Before going over Singleton patterns, lets first take a look at [http://jsbin.com/IBAbIgI/6/edit how constructors work]. | |||
== Lazy Initialization == | == Lazy Initialization == | ||
This example of a Singleton uses my patented "function rewriting" technique, where `getAnObject` is identifier is reassigned to the nested closure. | |||
<source lang="javascript"> | <source lang="javascript"> | ||
function getAnObject(a) { | function getAnObject(a) { | ||
Line 22: | Line 26: | ||
<source lang="javascript"> | <source lang="javascript"> | ||
// Not a Singleton. | // Not a Singleton. | ||
var | var C = function(a) { | ||
var b = a + 2; | var b = a + 2; | ||
this.name = b; | this.name = b; | ||
}; | }; | ||
var | var o1 = new C(3); | ||
var o2 = new C(4); | |||
var o3 = new C(5); | |||
</source> | </source> | ||
Example: Constructors and prototype inheritance. | Example: Constructors and prototype inheritance. | ||
[http://jsbin.com/IBAbIgI/6/edit jsbin] | [http://jsbin.com/IBAbIgI/6/edit jsbin] | ||
Line 35: | Line 42: | ||
<source lang="javascript"> | <source lang="javascript"> | ||
var anObject = new function(a) { | var anObject = new function(a) { | ||
// hidden variables. | |||
var b = a + 2; | var b = a + 2; | ||
this.name = b; | this.name = b; | ||
}(3); | }(3); | ||
alert(anObject.name); | |||
alert(typeof b); // "undefined" | |||
</source> | </source> | ||
Function scope allows us to hide variable `b`. | |||
[http://jsbin.com/ErAHEFo/1/edit jsbin] | [http://jsbin.com/ErAHEFo/1/edit jsbin] | ||
Object literal notation does not provide any information hiding mechanism: | |||
<source lang="javascript"> | |||
// exposed variables. | |||
var a = 3; | |||
var b = a + 2; | |||
var anObject = { | |||
name : b; | |||
}; | |||
</source> | |||
Example: [ | Example: [http://garretts.github.io/ape-javascript-library/build/anim/Animation.js APE Animation Manager] |
Latest revision as of 22:14, 7 March 2015
Singleton with information hiding.
Constructors[edit]
Before going over Singleton patterns, lets first take a look at how constructors work.
Lazy Initialization[edit]
This example of a Singleton uses my patented "function rewriting" technique, where `getAnObject` is identifier is reassigned to the nested closure. <source lang="javascript"> function getAnObject(a) {
var anObject;
var b = a + 1;
return (getAnObject = function() { if(! anObject ) { anObject = {name: b}; } return anObject; })();
} </source>
Eager Initialization[edit]
Not a Singleton, but a constructor. <source lang="javascript"> // Not a Singleton. var C = function(a) {
var b = a + 2; this.name = b;
};
var o1 = new C(3); var o2 = new C(4); var o3 = new C(5); </source>
Example: Constructors and prototype inheritance. jsbin
Singleton: <source lang="javascript"> var anObject = new function(a) { // hidden variables.
var b = a + 2; this.name = b;
}(3);
alert(anObject.name); alert(typeof b); // "undefined" </source>
Function scope allows us to hide variable `b`. jsbin
Object literal notation does not provide any information hiding mechanism: <source lang="javascript"> // exposed variables. var a = 3; var b = a + 2; var anObject = {
name : b;
}; </source>
Example: APE Animation Manager