Namespaces help to reduce the number of global variables required in your program, and it helps to avoid naming conflicts or too long prefix names.
An example of a namespace:
1 /**2 * Create global object MyApp3 * @module MYAPP4 * @title MYAPP Global5 */6 varMyApp = MyApp | | {};7 8 /**9 * Returns the specified namespace, or creates a namespace if the namespace does not exist. Ten * Note: Be careful when naming, keep keywords in mind, and may not be available in some browsers. One * A * @method namespace - * @param {String *} requires at least one namespace to be created - * References to objects created by the last namespace @return {object} the */ -Myapp.namespace =function(str) { - varParts = Str.split (".")), -Parent =MYAPP, +I=0, -L=0; + A if(parts[0]=== "MYAPP"){ atParts = Parts.slice (1); - } - for(I=0,l=parts.length; i<l;i++){ - if(typeofParent[parts[i]] = = = "undefined"){ -Parent[parts[i]] = {}; - } inParent =Parent[parts[i]]; - } to returnparent; + } - the /** * * Bfun is the abbreviation of basic Functions extended $ * Function: including array, string and so on function extensionPanax Notoginseng * - * @module Bfun the */ +Myapp.bfun = { AArray: (function(){ the return { + /** - * @method IsArray to determine if an array $ * @param an array of {array} $ * @return {Boolean} True returns True, otherwise it returns false - */ -IsArray:function(){ the returnObject.prototype.toString.call (arguments[0]) = = = ' [Object Array] '; - },Wuyi /** the * @method InArray Check if the value is in the array - * @param {Value,array} value, array Wu * @return {Boolean} True returns True, otherwise returns undefined - */ AboutInArray:function(Val,arr) { $ for(vari=0,l=arr.length;i<l;i++){ - if(Arr[i] = = =val) { - return true; - } A } + } the } - })(), $String: (function(){ the return { the /** the * @method Trim filters extra spaces on both sides of the string the * @param {string} string - * @return {string} string in */ theTrimfunction(){ the returnArguments[0].replace (/(^\s*) | ( \s*$)/g, ""); About }, the /** the * @method LTrim filter string to the left of the extra space the * @param {string} string + * @return {string} string - */ theLTrimfunction(){Bayi returnArguments[0].replace (/^s+/g, ""); the }, the /** - * @method RTrim filter string to the right of the extra space - * @param {string} string the * @return {string} string the */ theRTrimfunction(){ the returnArguments[0].replace (/s+$/g, ""); - } the } the })() the }94 the //Test theMyapp.test = { theInit:function(){98 //use the corresponding module to first reference About varMarray = Myapp.namespace ("MYAPP.bfun.array"); - varmstring = Myapp.namespace ("MYAPP.bfun.string");101 102 vararr = ["A", "B"];103 varstr = "ABC";104 theConsole.log ("Determine if an array:" +Marray.isarray (arr));106Console.log ("value is in array:" + Marray.inarray ("a", arr));107Console.log ("Filter left and right spaces:" +Mstring.trim (str));108 }109 } the 111MYAPP.test.init ();
About the namespace namespace pattern in JS