Last active
August 29, 2015 13:56
-
-
Save vanderlin/9053365 to your computer and use it in GitHub Desktop.
Simple Singleton JS
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 MyClass = (function() { | |
| var instance; | |
| function init() { | |
| // Private | |
| var className = "CLASS"; | |
| var cb; | |
| // public | |
| return { | |
| name:"todd", | |
| sayName:function() { | |
| if(cb) { | |
| cb(); | |
| } | |
| }, | |
| setCallback:function(callback) { | |
| cb = callback; | |
| } | |
| }; | |
| }; | |
| // Singleton Instance | |
| return { | |
| getInstance: function() { | |
| if(!instance) { | |
| instance = init(); | |
| } | |
| return instance; | |
| } | |
| }; | |
| })(); | |
| // this is just a ref to getInstance to use globaly | |
| var globalObject = MyClass.getInstance(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment