Created
January 3, 2012 19:04
-
-
Save ValeriiVasin/1556360 to your computer and use it in GitHub Desktop.
[javascript patterns] Singleton.
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
| // first realization: self-defining function | |
| var Universe = function () { | |
| var instance; | |
| Universe = function () { | |
| return instance; | |
| }; | |
| Universe.prototype = this; | |
| instance = new Universe(); | |
| instance.constructor = Universe; | |
| // other functionality below... | |
| instance.start = 0; | |
| instance.finish = 100; | |
| return instance; | |
| }; | |
| // second realization: immediate function | |
| var Universe; | |
| (function () { | |
| var instance; | |
| Universe = function () { | |
| if (instance) { | |
| return instance; | |
| } | |
| instance = this; | |
| this.start = 0; | |
| this.finish = 100; | |
| }; | |
| }()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment