Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 23rd, 2012  |  syntax: None  |  size: 0.74 KB  |  hits: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. // top-level namespace being assigned an object literal
  2. var myApp = myApp || {};
  3.  
  4. // a convenience function for parsing string namespaces and
  5. // automatically generating nested namespaces
  6. function extend( ns, ns_string ) {
  7.     var parts = ns_string.split("."),
  8.         parent = ns,
  9.         i;
  10.        
  11.     if ( parts[0] === "myApp" ) {
  12.         parts = parts.slice(1);
  13.     }
  14.    
  15.     for ( i = 0; i < parts.length; i++ ) {
  16.    
  17.         if ( typeof parent[parts[i]] == "undefined" ) {
  18.                 //create a property if it doesnt exist
  19.             parent[parts[i]] = {};
  20.         }
  21.        
  22.         parent = parent[parts[i]];
  23.     }
  24.    
  25.     return parent;
  26. }
  27.  
  28. // extend myApp with a deeply nested namespace  
  29. var mod = extend(myApp, 'myApp.modules.module2');