Created
September 24, 2013 18:06
-
-
Save christabor/6688875 to your computer and use it in GitHub Desktop.
JS Pattern: Try/catch a bunch of modules at once
This file contains 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
// a global module and namespace, preferably separated by individual files for each sub-module | |
var myGlobalObject = { | |
category: { | |
function1: function() {alert('function 1!');}, | |
function2: function() {alert('function 2!');}, | |
function3: function() {alert('function 3!');}, | |
function4: function() {alert('function 4!');} | |
} | |
}; | |
// the initialization loader | |
var moduleLoader = [ | |
myGlobalObject.category.function1, | |
myGlobalObject.category.function2, | |
myGlobalObject.category.function3, | |
myGlobalObject.category.function4 | |
]; | |
// do the magicka | |
for(var i = 0, len = moduleLoader.length; i <= len; i++) { | |
var fn = moduleLoader[i]; | |
try { | |
// try and run this function | |
fn(); | |
} | |
catch(e) { | |
// keep going | |
console.log(e); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Converting to array could be done simpler with map: $.map(myGlobalObject.category, function(obj){return obj;});