I. Principles
1.1 Wrapping the script with anonymous functions
1.2 Using namespaces (multilevel)
Ii. the process of improvement
2.1 Raw Data (A.js and b.js have global variable window.a, resulting in conflict, global variable belongs to window)
A.js
<script type= "Text/javascript" >
var a=123,b= "Hello World";
</script>
B.js
<script type= "Text/javascript" >
var a,c= "abc";
</script>
2.2 Using anonymous functions (A is not a global variable in a.js and b.js, but cannot be accessed in the B.js a.js B, no communication)
A.js
(Function ()
{
var a=123,b= "Hello World";
})();
B.js
(Function ()
{
var a,c= "abc";
})();
2.3 Using global variables for communication (using WINDOW.STR as a global variable, resulting in global variables becoming better and less maintainable)
var str;
A.js
(Function ()
{
var a=123,b= "Hello World";
Window.str=a;
})();
B.js
(Function ()
{
var a,c= "abc";
alert (WINDOW.STR);
})();
2.4 Using namespaces
var global={};
A.js
(Function ()
{
var a=123,b= "Hello World";
GLOBAL. A.a=a;
})();
//b.js
(function ()
{
var a,c= "abc";
Alert (GLOBAL. A.A);
}) ();