I've been experiementing with the (what i understand to be) the module pattern (or revealing module pattern) in Javascript. At the moment my main objective has been code organisation and cleaning up the global space in my projects but I'm sure there are a number of other benefits as well.
The basic pattern I've been following looks mostly like this :
var myThing = (function(w, $) {
var priv = {
// ... private methods
};
var public = {
// ... public methods
};
return public;
})(window, jQuery);
The main benefit of this that I can see is cleaner code and a better structure, by concealing the obj's internal methods and properties. Are there other benefits that are glaringly obvious that I'm not seeing? I'm really wondering when and when not to use this pattern.
Issue 1 : So one of the things I like is being able to pass in the w and $ as shorthand references to window and jQuery, but when I've tried to add another library to this it doesn't seem to work (specifically velocity.js).
EDIT : because I've included jQuery, velocity attaches its self to the global jQuery obj, the import is for globals which velocity is not, thus I can't import it the same way.
Issue 2 : Secondly I've read somewhere that its possible to make this pattern work regardless of load order, is that true and if so how should it be modified?
NB : Critically, I'm trying to understand this pattern and whats possible with it rather than substitutions such as implementing require or borwserify.
So after some back and fourth and some research, I've created the following little template that I use as a general starting place for working with the revealing module pattern.
https://gist.github.com/Pushplaybang/39ce46ec5b3d5502bb0c
The problem with loading velocity was simply that I was calling the object incorrectly.