r/webdev • • Dec 20 '15

4 JavaScript Design Patterns You Should Know

https://scotch.io/bar-talk/4-javascript-design-patterns-you-should-know
115 Upvotes

19 comments sorted by

View all comments

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:

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.

-4

u/[deleted] Dec 20 '15

Looks like a poor man's es6 import

2

u/jamauss Dec 20 '15

"poor man's" ? AFAIK the es6 import feature is supported in exactly zero browsers right now (Dec. 2015)

-2

u/[deleted] Dec 20 '15

Auto correct.

Yes, but babel is easy enough. Give it a try, so much more fun than es5