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

2)局部变量和全局变量 
马克-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>

更多请见:https://blog.csdn.net/qq_44594249/article/details/99864142

posted @ 2020-01-02 09:38  盖世神功  阅读(371)  评论(0编辑  收藏  举报