当前位置: 移动技术网 > IT编程>开发语言>JavaScript > javascript当中局部变量和全局变量

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

2020年01月02日  | 移动技术网IT编程  | 我要评论

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>

更多请见:

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网