Home  >  Article  >  Web Front-end  >  Examples to explain how to avoid JavaScript conflicts

Examples to explain how to avoid JavaScript conflicts

PHPz
PHPzOriginal
2016-05-16 15:22:09908browse

The example in this article explains how to avoid conflicts in javascript. Friends who need it can learn about it

[1] Engineer A writes function A

var a = 1;
var b = 2;
alert(a+b);//3

[2] Engineer B adds new function B

var a = 2;
var b = 1;
alert(a-b);//1

[3] In the previous step, Engineer B defined variable a with the same name without knowing it, causing a conflict. So use an anonymous function to wrap the script, so that the variable scope is controlled within the anonymous function.

//功能A
(function(){
  var a = 1;
  var b = 2;
  alert(a+b);//3
})();
//功能B
(function(){
  var a = 2;
  var b = 1;
  alert(a-b);//1
})();

[4] At this time, there are new requirements. Function C is added to the web page, and variable b in function A needs to be used. So define a global variable under the window scope and use it as a bridge to complete the communication between anonymous functions

//全局变量
var str;
//功能A
(function(){
  var a = 1;
  //将b的值赋给str
  var b = str = 2;
  alert(a+b);//3
})();
//功能B
(function(){
  var a = 2;
  var b = 1;
  alert(a-b);//1
})();
//功能C
(function(){
  //将str的值赋给b
  var b = str;
  alert(b);//2
})();

[5] But if function C We also need variable a in function A. At this time, we need to define another global variable

//全局变量
var str,str1;
//功能A
(function(){
  //将a的值赋给str1
  var a = str1 = 1;
  //将b的值赋给str
  var b = str = 2;
  alert(a+b);//3
})();
//功能B
(function(){
  var a = 2;
  var b = 1;
  alert(a-b);//1
})();
//功能C
(function(){
  //将str1的值赋给a
  var a = str1;
  //将str的值赋给b
  var b = str;
  alert(a*b);//2
})();

[6] But as anonymous functions need to communicate with each other, more variables are needed , the more global variables are needed. Therefore, it is necessary to strictly control the number of global variables. Using hash objects as global variables can use the required variables as attributes of the object, which can ensure that the number of global variables is small enough and the scalability is very good

//全局变量
var GLOBAL = {};
//功能A
(function(){
  //将a的值赋给GLOBAL.str1
  var a = GLOBAL.str1 = 1;
  //将b的值赋给GLOBAL.str
  var b = GLOBAL.str = 2;
  alert(a+b);//3
})();
//功能B
(function(){
  var a = 2;
  var b = 1;
  alert(a-b);//1
})();
//功能C
(function(){
  //将GLOBAL.str1的值赋给a
  var a = GLOBAL.str1;
  //将GLOBAL.str的值赋给b
  var b = GLOBAL.str;
  alert(a*b);//2
})();

[7] But if function D is added, function D needs to communicate with function B and use variable a in the function B script. The person who develops function D is engineer Ding

//全局变量
var GLOBAL = {};
//功能A
(function(){
  //将a的值赋给GLOBAL.str1
  var a = GLOBAL.str1 = 1;
  //将b的值赋给GLOBAL.str
  var b = GLOBAL.str = 2;
  alert(a+b);//3
})();
//功能B
(function(){
  //将a的值赋给GLOBAL.str1
  var a = GLOBAL.str1 = 2;
  var b = 1;
  alert(a-b);//1
})();
//功能C
(function(){
  //将GLOBAL.str1的值赋给a
  var a = GLOBAL.str1;
  //将GLOBAL.str的值赋给b
  var b = GLOBAL.str;
  alert(a*b);//2
})();
//功能D
(function(){
  //将GLOBAL.str1的值赋给a
  var a = GLOBAL.str1;
  alert(a*2);//4
})();

[8] Since Engineer Ding only cares about his own anonymous function and the anonymous function of function B, using GLOBAL.str accidentally overwrites the variable with the same name set in function A, causing an error in function C. . So we use namespaces to solve this problem. Under different anonymous functions, declare a different namespace according to the function. Then the attributes of the GLOBAL object in each anonymous function should not be directly hung on the GLOBAL object, but hung here. Under the namespace of the anonymous function

//全局变量
var GLOBAL = {};
//功能A
(function(){
  GLOBAL.A = {};
  //将a的值赋给GLOBAL.A.str1
  var a = GLOBAL.A.str1 = 1;
  //将b的值赋给GLOBAL.A.str
  var b = GLOBAL.A.str = 2;
  alert(a+b);//3
})();
//功能B
(function(){
  GLOBAL.B = {};
  //将a的值赋给GLOBAL.B.str1
  var a = GLOBAL.B.str1 = 2;
  var b = 1;
  alert(a-b);//1
})();
//功能C
(function(){
  //将GLOBAL.A.str1的值赋给a
  var a = GLOBAL.A.str1;
  //将GLOBAL.A.str的值赋给b
  var b = GLOBAL.A.str;
  alert(a*b);//2
})();
//功能D
(function(){
  //将GLOBAL.B.str1的值赋给a
  var a = GLOBAL.B.str1;
  alert(a*2);//4
})();

[9] If the program in the same anonymous function is very complex and has many variable names, the namespace can be further expanded to generate Secondary namespace

//以功能A为例
(function(){
  var a = 1, b = 2;
  GLOBAL.A = {};
  GLOBAL.A.CAT = {};
  GLOBAL.A.DOG = {};
  GLOBAL.A.CAT.name = 'mimi';
  GLOBAL.A.DOG.name = 'xiaobai';
  GLOBAL.A.CAT.move = function(){};
  GLOBAL.A.str1 = a;
  GLOBAL.B.str = b;  
})();

[10] Because generating namespace is a very common function, the function of generating namespace is further defined as a function for easy calling, and the complete version is rewritten The final code is as follows

var GLOBAL = {};
GLOBAL.namespace = function(str){
  var arr = str.split('.');
  var o = GLOBAL;
  var start = 0;
  if(arr[0] == 'GLOBAL'){
    start = 1;
  }else{
    start = 0;
  }
  for(var i = start; i < arr.length; i++){
    o[arr[i]] = o[arr[i]] || {};
    o = o[arr[i]];
  }
};
//功能A
(function(){
  var a = 1;
  var b = 2;
  GLOBAL.namespace(&#39;A.CAT&#39;);
  GLOBAL.namespace(&#39;A.DOG&#39;);
  GLOBAL.A.CAT.name = &#39;mimi&#39;;
  GLOBAL.A.DOG.name = &#39;xiaobai&#39;;
  GLOBAL.A.CAT.move = function(){};
  GLOBAL.A.str1 = a;
  GLOBAL.A.str = b;  
  alert(a+b);//3
})();
//功能B
(function(){
  var a = 2;
  var b = 1;
  GLOBAL.namespace(&#39;B&#39;);
  GLOBAL.B.str1 = a;
  alert(a-b);//1
})();
//功能C
(function(){
  var a = GLOBAL.A.str1;
  var b = GLOBAL.A.str;
  alert(a*b);//2
})();
//功能D
(function(){
  var a = GLOBAL.B.str1;
  alert(a*2);//4
})();

[11] The code conflict problem has been solved, but the maintainability is not strong. For example, now we need to ask engineer A to modify function B. Because the script written by Engineer A is about function A, he does not know the script situation of function B. In order to improve this situation, appropriate comments need to be added to the code.

var GLOBAL = {};
GLOBAL.namespace = function(str){
  var arr = str.split(&#39;.&#39;);
  var o = GLOBAL;
  var start = 0;
  if(arr[0] == &#39;GLOBAL&#39;){
    start = 1;
  }else{
    start = 0;
  }
  for(var i = start; i < arr.length; i++){
    o[arr[i]] = o[arr[i]] || {};
    o = o[arr[i]];
  }
};
/*
* @method 功能A:实现加法运算
* @author 工程师甲
* @connect 1234567
* @time 2015-01-01
*/

(function(){
  var a = 1;
  var b = 2;
  GLOBAL.namespace(&#39;A.CAT&#39;);
  GLOBAL.namespace(&#39;A.DOG&#39;);
  GLOBAL.A.CAT.name = &#39;mimi&#39;;
  GLOBAL.A.DOG.name = &#39;xiaobai&#39;;
  GLOBAL.A.CAT.move = function(){};
  GLOBAL.A.str1 = a;
  GLOBAL.A.str = b;  
  alert(a+b);//3
})();
/*
* @method 功能B:实现减法运算
* @author 工程师乙
* @connect 1234567
* @time 2015-01-01
*/
(function(){
  var a = 2;
  var b = 1;
  GLOBAL.namespace(&#39;B&#39;);
  GLOBAL.B.str1 = a;
  alert(a-b);//1
})();
/*
* @method 功能C:实现乘法运算
* @author 工程师丙
* @connect 1234567
* @time 2015-01-01
*/
(function(){
  var a = GLOBAL.A.str1;
  var b = GLOBAL.A.str;
  alert(a*b);//2
})();
/*
* @method 功能D:实现乘2运算
* @author 工程师丁
* @connect 1234567
* @time 2015-01-01
*/
(function(){
  var a = GLOBAL.B.str1;
  alert(a*2);//4
})();

To prevent javascript from conflicting, you need to

  •   [1] Avoid The proliferation of global variables

  •  [2] Reasonable use of namespaces

  •  [3] Add necessary comments to the code

The above is the detailed content of this article, I hope it will be useful for everyone’s learning help.

【Recommended related tutorials】

1. JavaScript video tutorial
2. JavaScript online manual
3. bootstrap tutorial

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn