Created
August 13, 2014 03:14
-
-
Save wulymammoth/a99a00a48bbb7656b0ec to your computer and use it in GitHub Desktop.
Singleton Constructor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var MakeSingleton = function(){ | |
| var obj; | |
| return function() { | |
| if( obj ){ | |
| return obj; | |
| } else{ | |
| obj = this; | |
| } | |
| }; | |
| }; | |
| var Singleton = MakeSingleton(); | |
| var x = new Singleton(); | |
| var y = new Singleton(); | |
| x === y; | |
| // var x = new Singleton(); | |
| // var y = new Singleton(); | |
| // x === y => true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment