namespace function code

window.namespace = function (path, fn) {
  var current = window;
  
  //Only parse string paths 
  if (typeof path != 'string') {
    current = path;
  }
  else {
    var spaces = path.split('.');
				
    //Set up the namespace objects
    for (var i = 0, length = spaces.length; i < length; i++) {
				
      if (current[spaces[i]] == null) current[spaces[i]] = {};
      current = current[spaces[i]];
    }
  }
			
  //Now call the function
  if (fn != null) fn.call(current);
  return current;
};

Advertisements

One thought on “namespace function code

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s