Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Updated singleton pattern example

  • Loading branch information...
commit 7e6d70bdbaa67836d2797a41fec1130ecfcdb20b 1 parent 37e41ae
Francois authored
Showing with 9 additions and 2 deletions.
  1. +9 −2 book/index.html
View
11 book/index.html
@@ -1478,6 +1478,8 @@ <h2 id="singletonpatternjavascript">
var privateVariable = "Im also private";
+ var privateRandomNumber = Math.random();
+
return {
// Public methods and variables
@@ -1485,7 +1487,12 @@ <h2 id="singletonpatternjavascript">
console.log( "The public can see me!" );
},
- publicProperty: "I am also public"
+ publicProperty: "I am also public",
+
+ getRandomNumber: function() {
+ return privateRandomNumber;
+ }
+
};
};
@@ -1511,7 +1518,7 @@ <h2 id="singletonpatternjavascript">
var singleA = mySingleton.getInstance();
var singleB = mySingleton.getInstance();
-console.log( singleA === singleB ); // true
+console.log( singleA.getRandomNumber() === singleB.getRandomNumber() ); // true
</pre>
<p>What makes the Singleton is the global access to the instance (generally through <code>MySingleton.getInstance()</code>) as we don't (at least in static languages) call <code>new MySingleton()</code> directly. This is however possible in JavaScript.</p>
Please sign in to comment.
Something went wrong with that request. Please try again.