Something that I think really should have been pointed out in this article is that it's helpful (and very common actually) to pass in arguments to your IIFE's so that you can use functionality from other libraries inside your own modules as well as split up namespaced functionality across multiple .js files. For example:
var myModule = (function($, lib) {
lib.myModuleFunction = function() {
// do some jQuery stuff with the same $ object syntax you're used to anywhere else
$('#IdSelector').show();
};
return lib;
})(jQuery, myModule || {});
I can now have a myModule reference spread across multiple .js files and as long as I follow this pattern in each file, all of the lib.xxxx functions inside my modules will be available as myModule.xxxx across the entire js codebase.
3
u/jamauss Dec 20 '15
Something that I think really should have been pointed out in this article is that it's helpful (and very common actually) to pass in arguments to your IIFE's so that you can use functionality from other libraries inside your own modules as well as split up namespaced functionality across multiple .js files. For example:
I can now have a myModule reference spread across multiple .js files and as long as I follow this pattern in each file, all of the lib.xxxx functions inside my modules will be available as myModule.xxxx across the entire js codebase.