
Untitled
By: a guest on
May 23rd, 2012 | syntax:
None | size: 0.74 KB | hits: 15 | expires: Never
// top-level namespace being assigned an object literal
var myApp = myApp || {};
// a convenience function for parsing string namespaces and
// automatically generating nested namespaces
function extend( ns, ns_string ) {
var parts = ns_string.split("."),
parent = ns,
i;
if ( parts[0] === "myApp" ) {
parts = parts.slice(1);
}
for ( i = 0; i < parts.length; i++ ) {
if ( typeof parent[parts[i]] == "undefined" ) {
//create a property if it doesnt exist
parent[parts[i]] = {};
}
parent = parent[parts[i]];
}
return parent;
}
// extend myApp with a deeply nested namespace
var mod = extend(myApp, 'myApp.modules.module2');