Created
November 22, 2012 23:26
-
-
Save f0t0n/4133310 to your computer and use it in GitHub Desktop.
JS Namespace example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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