javascript当中局部变量和全局变量

局部变量和全局变量
马克- to-win:马克 java社区:防盗版实名手机尾号: 73203。
马克-to-win:浏览器里面 window 就是 global,通常可以省。

nodejs 里没有 window,但是有个叫 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里面时,var是局部的,而无var时是代表全局的*/ 
    var testVar = "全量";
    document.writeln("window.testVar is" + window.testVar+testVar);
    var testqVar = "全量q";
/*如不屏蔽下句话,程序直接停在这了,因为出错了,不认识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>




输出结果:

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>

输出结果:
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>

输出结果:
全量全量




例 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的作用范围是整个函数内,而不是循环体内
                document.writeln(z);
            }
        //即使出了循环体,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>


输出结果:

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>



输出结果:

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