public
Created

JS Namespace example

  • Download Gist
base.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
(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);

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.