varGLOBAL = {};//' str ': ' GLOBAL. A ', ' a ', ' a.str1 'Global.namespace =function(str) {//Split string split into arrays vararr = Str.split ('. ')); varo =GLOBAL; varStart = 0; //determines whether the first element in the array is global; //if so, the for loop below starts from the second array element, otherwise, starts from scratch if(arr[0] = = ' GLOBAL ') {Start= 1; } esle {Start= 0; } //o = o[arr[i]]: If ARR is longer than or equal to 2, in order to form a nested hierarchical relationship, O is always equal to the upper-level object, so o[arr[i]] will always add child objects for the upper-level object. // || {}: Determines whether the attribute exists at that level, if it exists, the value is not changed, and, conversely, an object is paid to the value for(vari = start; i < arr.length; i++) {O[arr[i]]= O[arr[i]] | | {}; o=O[arr[i]]; }};/*@method Function A: implementing addition @author engineer a @connect 1234567 @time 2015-01-01*/(function() { varA = 1,b = 2; Global.namespace (' A.cat '); Global.namespace (' A.dog '); GLOBAL. A.cat.name= ' Mimi '; GLOBAL. A.dog.name= ' Xiaobai '; GLOBAL. A.cat.move=function() {}; GLOBAL. A.STR1=A; GLOBAL. A.str=b;}) ();/*@method Function B: implement addition @author engineer B @connect 1234567 @time 2015-01-01*/(function() { varA = 2; varb = 1; Global.namespace (B); GLOBAL. B.STR1=A; Alert (A-B);//1})();/*@method Function C: implement addition operation @author Engineer @connect 1234567 @time 2015-01-01*/(function() { varA =GLOBAL. A.STR1; varb =GLOBAL. A.STR; Alert (A*B);//2})();/*@method function D: Implementation of addition operations @author engineer Ding @connect 1234567 @time 2015-01-01*/(function() { varA =GLOBAL. B.STR1; Alert (A* *);//4})();
In cooperative development, the most prone to variable confusion, using objects to store all variables is a more popular method, but simple object storage is difficult to deal with the many complex operations of global variables, therefore, according to different functions named global variables of the properties of the formation of a special level (the first level of global variables), Variables with different functions are divided according to the first-level attribute name, and a function method is used to carry tedious naming (simplifying the code) to annotate to enhance the maintainability of the code.
Cut to the blue ideal of small matches, if there is infringement, please leave a message, will be deleted immediately.
JavaScript co-development