I'm trying to use a modular namespace pattern which allows me to expand the functions available within various namespaces and define them across multiple files. This is the pattern I'm using;
var Namespace = Namespace || {};
Namespace.SubSpace = (function () {
var subspace = {};
subspace.Func = function () {
return "My Function";
};
return subspace;
})();
How can I make my namespace functions available prior to their definition? In other words how can I call Namespace.SubSpace.Func()
without it relying on the function declaration having already been parsed?
I can get close enough to solving my problem by using an approach similar to JQuery's
ready()
function. However, it does rely on defining the root namespace before anything else. The root namespace also includes functionality which allows callbacks to be registered.Importantly this distinguishes between callbacks which should run before others. This allows framework modules to define sub-namespaces and functions before user scripts are executed. (Even if user scripts are loaded first).
User Scripts
Framework Module
Finally, calling
Namespace.Begin()
executes everything in the correct order. Also, since I'm using JQuery anyway, I can wrap this in$(document).ready()
.I'm not terribly proficient in javascript so I'm not sure how 'correct' this approach is, I'd welcome comments & suggestions.