Skip to content

Instantly share code, notes, and snippets.

@f0t0n
Created November 22, 2012 23:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save f0t0n/4133310 to your computer and use it in GitHub Desktop.
Save f0t0n/4133310 to your computer and use it in GitHub Desktop.
JS Namespace example
(function($, window) {
/**
* Provides a namespace for BugKick classes
*/
var _ = window[baseNameSpace] = window[baseNameSpace] || {};
_.jQuery_URL = 'https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js'
/**
*
* @param namespace {@string}
*/
_.namespace = function(namespace) {
var branch = namespace.split('.'),
path = _;
if(branch.length > 0 && branch[0] == baseNameSpace) {
branch.shift();
}
for(var i = 0, node; node = branch[i++];) {
path[node] = path[node] || {};
path = path[node];
}
return path;
};
/* Namespace definitions */
_.string = _.namespace('string');
_.url = _.namespace('url');
_.page = _.namespace('page');
/****************************/
/**
* Concatenates string expressions. This is useful
* since some browsers are very inefficient when it comes to using plus to
* concat strings. Be careful when using null and undefined here since
* these will not be included in the result. If you need to represent these
* be sure to cast the argument to a String first.
* For example:
* <pre>buildString('a', 'b', 'c', 'd') -> 'abcd'
* buildString(null, undefined) -> ''
* </pre>
* @param {...*} var_args A list of strings to concatenate. If not a string,
* it will be casted to one.
* @return {string} The concatenation of {@code var_args}.
*/
_.string.buildString = function(var_args) {
return Array.prototype.join.call(arguments, '');
};
})(jQuery, window);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment