function fn(){ var a= b = 2; alert(a); // 2; } alert(a);//未被定义; alert(b);//全局变量;b=2;
var a = 1; function fn1(){ var a = 5; function fn2(){ alert(a); // a=5; } fn2(); } alert(a); // a=1; fn1(); //这个就是区块域;
给定一个实例:function fn1(){ var a = 5; function fn2(){ function fn3(){ alert(a); a = 10; } fn3(); } fn2();
alert(a); } fn1();
运行程序是这么运行的:function fn1(){ function fn2(){ function fn3(){ alert(a); //这里a = 5; a = 10; } fn3(); } var a = 5; fn2();
alert(a); //这里a = 10; } fn1();
alert(a);