Javascript String namespace(separator)

Description

Javascript String namespace(separator)


String.prototype.namespace = function(separator) {
  this.split(separator || '.').inject(window, function(parent, child) {
    return parent[child] == undefined ? parent[child] = { } : parent[child];
  });/*from ww w  . j av a 2  s  .  c o  m*/
}

Javascript String namespace(separator)

String.prototype.namespace = function(separator) {
    var ns = this.split(separator || '.'), p = window, i;
    for (i = 0; i < ns.length; i++) {
  p = p[ns[i]] = p[ns[i]] || {};/* w w  w.j  a  v a2  s  .c  o m*/
    }
};

//define legal namespaces here:
'localground'.namespace();
'localground.map'.namespace();
'localground.map.tiles'.namespace();

Javascript String namespace(separator)

/**/* w ww . jav a  2s.  c o m*/
 * Allow using namespace.
 * http://blogger.ziesemer.com/2008/05/javascript-namespace-function.html
 */
String.prototype.namespace = function(separator) {
 var ns = this.split(separator || '.'),
     o = window,
     i,
     len;
 for (i = 0, len = ns.length; i < len; i++) {
  o = o[ns[i]] = o[ns[i]] || {};
 }
 return o;
};



PreviousNext

Related