Analysis of namespace mode in JavaScript, namespace
Namespace is the namespace, also known as "namespace" and "namespace ". Unlike C # or Java, JavaScript has special namespace and package syntax support. When JavaScript is complicated to a certain extent, especially after a large number of third-party JS frameworks and class libraries are referenced, naming conflicts will become a serious problem. Therefore, it is important to use JS's own workarounds to create namespaces.
Namespace helps reduce the number of global variables required by the program, and also helps avoid name conflicts or long name prefixes.
Example of a namespace:
/*** Create a Global Object MYAPP * @ module MYAPP * @ title MYAPP Global */var MYAPP = MYAPP ||{};/*** return the specified namespace, if the namespace does not exist, create a namespace. * Note: Be careful when naming. Note that keywords are retained and may not be available in some browsers. ** @ Method namespace * @ param {String *} must at least create a namespace * @ return {Object} reference to the Object created in 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 extensions include arrays, strings, and so on. ** @ module bfun */MYAPP. bfun = {array :( function () {return {/*** @ method isArray determines whether it is an Array * @ param {array} Array * @ return {Boolean} returns true, otherwise, false */isArray: function () {return Object is returned. prototype. toString. call (arguments [0]) ===' [object Array] ';},/*** @ method inArray checks whether the value is in the Array * @ param {value, array} value. Array * @ return {Boolean} 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 filter redundant spaces on both sides of the String * @ param {String} String * @ return {String} String */trim: function () {return arguments [0]. replace (/(^ \ s *) | (\ s * $)/g ,"");}, /*** @ method ltrim filter extra spaces on the left of the String * @ param {String} String * @ return {String} String */ltrim: function () {return arguments [0]. replace (/^ s +/g ,"");}, /*** @ method rtrim filter unnecessary spaces on 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 first. namespace ("MYAPP. bfun. array "); var mstring = MYAPP. namespace ("MYAPP. bfun. string "); var arr = [" a "," B "]; var str =" abc "; console. log ("judge whether it is an array:" + marray. isArray (arr); console. log ("Whether the value is in the array:" + marray. inArray ("a", arr); console. log ("filter left and right spaces:" + mstring. trim (str);} MYAPP. test. init ();
The above is a full description of the namespace mode in JavaScript described in the editor. I hope it will be helpful to you. If you have any questions, please leave a message for me, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!