namespace is "namespace", also known as "namespace" and "namespace". JavaScript is not like C# or Java, with special namespace and package syntax support. When JS is complex to a certain extent, especially when referring to a large number of third-party JS frameworks and class libraries, naming conflicts will become a serious problem. Therefore, it is important to use JS's own workaround to build a namespace.
Namespaces help reduce the number of global variables required in the program, and also help avoid naming conflicts or excessively long name prefixes.
Examples about namespaces:
/*** Create global object MYAPP* @module MYAPP* @title MYAPP Global*/var MYAPP = MYAPP || {};/*** Returns the specified namespace, and creates a namespace if the namespace does not exist. * Note: Be careful when naming and keeping keywords, which may not be used in some browsers. ** @method namespace* @param {String *} At least one namespace needs to be created* @return {Object} Reference to the object created by the last namespace*/MYAPP.namespace = function(str){var parts = str.split("."),parent = MYAPP,i=0,l=0;if(parts[0]==="MYAPP"){parts = parts.slice(1);}for(i=0,l=parts.length; i<l;i++){if(typeof parent[parts[i]] == "undefined"){parent[parts[i]] = {};}parent = parent[parts[i]];}return parent;}/*** bfun is the abbreviation of Basic Functions Extended* Function: Including arrays, strings, etc. Function extension** @module bfun*/MYAPP.bfun = {array:(function(){return {/*** @method isArray Determine whether it is an array* @param {Array} Array* @return {Boolean} True returns true, otherwise it returns false*/isArray: function(){return Object.prototype.toString.call(arguments[0]) === '[object Array]'; },/*** @method inArray Check whether the value is in the array* @param {value, Array} value, array* @return {Boolean} True returns true, otherwise undefined*/inArray: function(val,arr){for(var i=0,l=arr.length;i<l;i++){if(arr[i] === val){return true;}}}}})(),string:(function(){return {/*** @method trim Filters extra spaces on both sides of the string* @param {String} String* @return {String} String*/trim: function(){return arguments[0].replace(/(^/s*)|(/s*$)/g, "");},/*** @method ltrim Filters extra spaces to the left of the string* @param {String} String* @return {String} String*/ltrim: function(){return arguments[0].replace(/^s+/g, "");},/*** @method rtrim Filters extra spaces to the right of the string* @param {String} String* @return {String} String*/rtrim: function(){return arguments[0].replace(/s+$/g, "");}}})()}// Test MYAPP.test = {init: function(){// Use the corresponding module to reference var marray = MYAPP.namespace("MYAPP.bfun.array");var mstring = MYAPP.namespace("MYAPP.bfun.string");var arr = ["a","b"];var str = "abc ";console.log("Defaults whether it is an array: " + marray.isArray(arr));console.log("Is the value in the array: " + marray.inArray("a",arr));console.log("Filter left and right spaces: " + mstring.trim(str));}}MYAPP.test.init();The above is the complete description of the namespace namespace pattern in JavaScript introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!