javascript當(dāng)中局部變量和全局變量
局部變量和全局變量
馬克- to-win:馬克 java社區(qū):防盜版實(shí)名手機(jī)尾號(hào): 73203。
馬克-to-win:瀏覽器里面 window 就是 global,通??梢允 ?br>
nodejs 里沒有 window,但是有個(gè)叫 global 的。
例 3.2.1
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
</head>
<script>
/* 馬克-to-win:有var無var, 在function外是一樣的,都是全局的,在function里面時(shí),var是局部的,而無var時(shí)是代表全局的*/
var testVar = "全量";
document.writeln("window.testVar is" + window.testVar+testVar);
var testqVar = "全量q";
/*如不屏蔽下句話,程序直接停在這了,因?yàn)槌鲥e(cuò)了,不認(rèn)識(shí)testGlobal,得把下一句和下下句換一下位置,就ok了 */
// document.writeln("testGlobal is" + testGlobal);
testGlobal = "全量global";
document.writeln("abc is" + abc);
var abc;
testGlobalInVar = "全量globalInVar";
function testSco()
{
var lll = "qqq";
var testVar = "局量"; //此testVar非外面的testVar
testqVar = "全量qchange"; //此testqVar就是外面的testqVar
testGlobal = "全量globalchange";
var testGlobalInVar = "局量global";//此testGlobalInVar非外面的testGlobalInVar
/*local variable is stronger than global variable.so "testVar" in the following statement means local variable.*/
document.writeln(testVar);
document.writeln(testqVar);
document.writeln("testGlobalInVar is " + testGlobalInVar);
}
testSco();
document.writeln("second test is " + testVar);
document.writeln("second testqVar is " + testqVar);
document.writeln("testGlobal is " + testGlobal);
document.writeln("testGlobalInVar is " + testGlobalInVar);
</script>
輸出結(jié)果:
window.testVar is全量全量 abc isundefined 局量全量qchange testGlobalInVar is 局量global second test is 全量 second testqVar is 全量qchange testGlobal is 全量globalchange testGlobalInVar is 全量globalInVar
例 3.2.2
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
</head>
<script>
var abc = "全量";
function testscope()
{
/* refer to 上例 first. 馬克-to-win: because already defined local variable, but does not assign value, so it is undefined. If we totally remove var abc = "局量"; alert(abc);
will print out 全局變量, but local "scope" will override the global "scope", it is undefined. so we can learn a lesson, if we use a local variable, we must first assign value, then we can use it.
*/
document.writeln(abc);
var abc = "局量";
document.writeln(abc);
}
testscope();
document.writeln(abc);
</script>
輸出結(jié)果:
undefined 局量全量
例 3.2.2_1:
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
</head>
<script>
var abc = "全量";
function testscope()
{
document.writeln(abc);
}
testscope();
document.writeln(abc);
</script>
輸出結(jié)果:
全量全量
例 3.2.3(noblockScope)
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
</head>
<script>
function test()
{
/* 馬克-to-win: there are only global variable (outside of any function) and function variable, no block variable at all.*/
/*note that in javascript, there is no block variable, only function variable, so m and z are function variable.*/
for (var z = 0; z < 2; z++)
{
var m = 1;
//m的作用范圍是整個(gè)函數(shù)內(nèi),而不是循環(huán)體內(nèi)
document.writeln(z);
}
//即使出了循環(huán)體,m和z的值依然存在
document.writeln(z + " is z"+m+" is m" );
}
/*document is a property of global object---window,so can directly use.*/
test();
document.writeln("馬克-to-win");
/*the following statement is wrong because no global m is defined first, so nothing can be printed out*/
document.writeln(m + "is m");
document.writeln("馬克-to-win");
</script>
輸出結(jié)果:
0 1 2 is z1 is m 馬克-to-win
例 3.2.4(局部變量,全局變量)
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
</head>
<script>
function M1(qqqv, qixyv)
{
/*馬克-to-win:note that here if var m1=new M1(8,9); then this.qqq means m1's property, while
if you directly call M1(2,3); then this.qqq means window's property. */
this.qqq = qqqv;
/*note that here "qixy" means the windows's property*/
qixy = qixyv;
/*the following "q" is a local variable, so inside another function, it can not be called. */
var q = 3;
}
function M2()
{
document.writeln("qixy is" + qixy);
document.writeln("window.qixy is" + window.qixy);
document.writeln("this.qixy is" + this.qixy);
document.writeln("this.qqq is" + this.qqq);
//alert("q is"+ q);
}
var m1 = new M1(8, 9);
m1.info = function()
{
document.writeln("inside m1.info this.qqq is " + this.qqq);
};
m1.info();
/*note that the following mk.info(); must be commented out, otherwise, it report error, because mk does not have
info() function, because the fucntion of info only belong to m1. this is also the difference between prototype
and m1.info(); */
// var mk=new M1(8,9);
// mk.info();
M2();
document.writeln("this的qqq屬性為" + this.qqq);
document.writeln("this的qixy屬性為" + this.qixy);
document.writeln("m1的qqq屬性為" + m1.qqq);
document.writeln("the second step");
M1(2, 3);
M2();
document.writeln("this的qqq屬性為" + this.qqq);
document.writeln("this的qixy屬性為" + this.qixy);
</script>
</html>
輸出結(jié)果:
inside m1.info this.qqq is 8 qixy is9 window.qixy is9 this.qixy is9 this.qqq isundefined this的qqq屬性為undefined this的qixy屬性為9 m1的qqq屬性為8 the second step qixy is3 window.qixy is3 this.qixy is3 this.qqq is2 this的qqq屬性為2 this的qixy屬性為3