Created
January 23, 2013 13:06
-
-
Save flyingzl/4605351 to your computer and use it in GitHub Desktop.
javascript singleton pattern
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 Singleton =(function(){ | |
| var _instance ; | |
| function init(){ | |
| return { | |
| method: function(){ | |
| return 1; | |
| }, | |
| property: 'hello' | |
| } | |
| } | |
| return { | |
| getInstance: function(){ | |
| !_instance && ( _instance = init() ); | |
| return _instance; | |
| } | |
| } | |
| })(); | |
| Singleton.getInstance().method(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment