The way you do it right now, exactly the way I do it, I usually create window objects inside the anonymous function itself and then declare it inside it (in this case: jclass = window.jClass).
(function (jClass, $, undefined) { /// <param name="$" type="jQuery" /> var VERSION = '1.31'; UPDATED_DATE = '7/20/2012'; // Private Namespace Variables var _self = jClass; // internal self-reference jClass = window.jClass; // (fix for intellisense) $ = jQuery; // save rights to jQuery (also fixes vsdoc Intellisense) // I init my namespace from inside itself $(function () { jClass.init('branchName'); }); jClass.init = function(branch) { this._branch = branch; this._globalFunctionality({ globalDatePicker: true }); this._jQueryValidateAdditions(); //put GLOBAL IMAGES to preload in the array this._preloadImages( [''] ); this._log('*******************************************************'); this._log('jClass Loaded Successfully :: v' + VERSION + ' :: Last Updated: ' + UPDATED_DATE); this._log('*******************************************************\n'); }; jClass._log = function() { //NOTE: Global Log (cross browser Console.log - for Testing purposes) //ENDNOTE try { console.log.apply(console, arguments); } catch (e) { try { opera.postError.apply(opera, arguments); } catch (e) { /* IE Currently shut OFF : alert(Array.prototype.join.call(arguments, ' '));*/ } } }; }(window.jClass= window.jClass|| {}, jQuery));
The reason I leave them completely anonymous like this is because even in a different file I want to add much more functionality to this jClass. I just create another:
(function jClass, $, undefined) { jClass.newFunction = function (params) {
As you can see, I prefer the object.object notation, but you can use the object literal object: object, it up to you!
In any case, leaving it all separate and encapsulating without the actual page logic, this will simplify this in the globalJS file, and every page on your site will be able to use it. For example, an example below.
jClass._log('log this text for me');
You do not want to intertwine the model logic with your business logic, so yours on the right path separating the two and allowing your global namespace / class / etc to be more flexible!
source share