Skip to content

Instantly share code, notes, and snippets.

@christabor
Created September 24, 2013 18:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save christabor/6688875 to your computer and use it in GitHub Desktop.
Save christabor/6688875 to your computer and use it in GitHub Desktop.
JS Pattern: Try/catch a bunch of modules at once
// 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);
}
}
@christabor
Copy link
Author

Converting to array could be done simpler with map: $.map(myGlobalObject.category, function(obj){return obj;});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment