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
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/158698.html
標籤:JavaScript
上一篇:webpack 打包增加版本資訊
