6

Init + Module Pattern JS

 1 year ago
source link: https://gist.github.com/nathansmith/274388
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

h3h commented Apr 25, 2011

Why alias window and document? And what's that undefined for in the argument list?

alias them for easy munging, and more clear use of globals. undefined isn't passed in, but is set as a parameter so when the function runs, it is guaranteed to be undefined.

Protects you from: undefined = true

Author

nathansmith commented Apr 25, 2011

Yeah, that way window, document, jQuery, and undefined get shrunk to one-letter variables with it's minified...

var APP=(function(a,b,c,d){/*codez*/})(jQuery,this,this.document)

h3h commented Apr 25, 2011

Crazy. Those hacks make me wish JS-in-the-browser provided a more reasonable programming environment. Oh well.

Any specific reason you make all of the methods of the module public (by returning the whole object) instead of only exposing those that require public access?

I also can't think of a case where multiple init methods would be better than just a single setup() method that calls various helper methods to set things up.

Here's the skeleton of what I usually use:

/**
  Description here
  2011-04-25 / ([email protected])
**/
if (typeof (Gowalla) === 'undefined') Gowalla = {};

Gowalla.PageOrComponentName = (function ($) {

  // *-* public methods *-*

  var setup = function () {
    $("#header").click(eventHeaderClicked);
  };

  // *-* utility methods *-*

  var doThing = function (x) {
    return x + x;
  };

  // *-* event methods *-*

  var eventHeaderClicked = function () {
    doThing(this.attr('id'));
  };

  // expose public methods
  return {
    setup: setup
  };
})(jQuery);

jQuery(document).ready(Gowalla.PageOrComponentName.setup);

Author

nathansmith commented Apr 25, 2011

@h3h - Agreed. It's insane that window, document, and undefined are mutable in JavaScript :)

Author

nathansmith commented Oct 18, 2011

@h3h - I just realized (coming back to this to show a coworker), that I never answered your question about why making methods public. I do that so that they can be run on page load automatically, but then can be run selectively if need be. Let's say you want to bind something to an element on page load, but then via Ajax insert another set of similar elements. You could be just jQuery's .live(), but maybe there's some other logic that needs to run.

In that case, the functions inside the init could do something along the lines of...

$(/* selector */).unbind('click.namespace').bind('click.namespace', function() {...});

But, it's really a "6-of-one, half-dozen of another" type situation, I agree. :)

Thank You for very important pattern.

sukima commented Jul 26, 2016

alias them for easy munging, and more clear use of globals

I honestly don't know what that sentence means. I get the words but I am not seeing the meaning. What is more clear use of globals trying to say? Is not window already a global?

Protects you from: undefined = true

In all my years this has never happened to me. Why do we need to protect against it? I can think of hypothetical cases where it could be exploited but to be honest in the grand scheme of things does this actually happen enough that coders have to activly work around it?

(Keep in mind I'm not advocating disusing it. It is still a good pattern. I'm simply asking for more facts checks on the exploit ability and frequency of such use cases in the wild.)

window, document, jQuery, and undefined get shrunk to one-letter variables with it's minified

Ahh, that makes really cleaver sense. In fact I'd argue that a good minifier worth its salt would wrap the code in this module pattern for you.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK