//全局变量 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 })();
//全局变量 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 })();
//全局变量 varGLOBAL = {}; //功能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 })();
//全局变量 varGLOBAL = {}; //功能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 })();
//全局变量 varGLOBAL = {}; //功能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 })();