How to make JS not conflict, avoid the flood of global variables, use namespaces rationally

Source: Internet
Author: User

To avoid overwriting and collisions between variables, namespaces can be generated, and namespaces are a special prefix that is implemented in JS with {} objects.

In different anonymous functions, a different namespace is declared according to function, and the properties of the global object in each anonymous function are not directly attached to global, but are hung under the namespace of the secondary anonymous function, such as:

  1. < Script type="text/javascript">
  2. var GLOBAL={}
  3. </ Script >   
  4. < Script type="text/javascript">
  5. (function () {
  6. var a=123, a1=N;
  7. GLOBAL. A={}
  8. GLOBAL.  A.str=a;
  9. })();
  10. </ Script >   
  11. < Script type="text/javascript">
  12. (function () {
  13. var b1=123, b2=N;
  14. GLOBAL. B={}
  15. GLOBAL.  B.str=a;
  16. })();
  17. </ Script >   

If the program in the same anonymous function is very complex, with many variable names, the namespace can be further extended to generate a level two namespace:

  1. < Script type="text/javascript">
  2. var GLOBAL={}
  3. </ Script >   
  4. < Script type="text/javascript">
  5. (function () {
  6. var a=123, a1=N;
  7. GLOBAL.  A={};
  8. GLOBAL.  A.cat={};
  9. GLOBAL.  A.dog={};
  10. GLOBAL.  A.cat.name="Mini";
  11. GLOBAL. A.cat.move=function() {
  12. }
  13. GLOBAL.  A.dog.name="Mini";
  14. GLOBAL. A.dog.move=function() {
  15. }
  16. })();
  17. </ Script >   


Because building namespaces is a very common feature, you can further define the functionality that generates namespaces as a function for easy invocation, as follows:

  1. < Script type="text/javascript">
  2. var GLOBAL={}
  3. Global.namespace = function (str) {
  4. var arr=str. Split ("."), o=GLOBAL   ;
  5. For (i=arr[0]== "GLOBAL"? 1:0;i<Arr.length ; i++) {  
  6. O[arr[i]]=o[arr[i] | | {};
  7. o = o  [Arr[i]];
  8. }
  9. }
  10. </ Script >   


To invoke a namespace-specific action:

  1. < Script type="text/javascript">
  2. //=============================================================
  3. Function A
  4. Engineer Armor
  5. Email:[email protected] Msn:[email protected] "
  6. 2012-11-06
  7. //=============================================================
  8. (function () {
  9. var a=123, a1="Hello World";
  10. Global.namespace ("A.cat");
  11. Global.namespace ("A.dog");
  12. GLOBAL.  A.cat.name="Mini";
  13. GLOBAL. A.cat.move=function() {
  14. }
  15. GLOBAL.  A.dog.name="Mini";
  16. GLOBAL. A.dog.move=function() {
  17. }
  18. GLOBAL.  A.str=a;
  19. GLOBAL.  A.STR1=A1;
  20. })();

Similar in turn, no matter how many people direct team development, or personal indirect team work, need good maintainability.

1. Add the necessary code comments

2, let JS do not conflict, you need to avoid the flooding of global variables, reasonable use of namespaces

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.