我在理解下面的全局变量x的生命周期时遇到了一些麻烦。我已经在线上评论了我不理解的问题..请帮助...谢谢
var target = document.getElementById("outputArea");
var outString = "";
var x = 0;
var y = 0;
callMeOften2();
callMeOften2();
callMeOften2();
outString += "<br/>";
target.innerHTML = outString;
function callMeOften2() {
outString += x + "<br/>"; //why isn't this going to give an output of 0? but gave an output of undefined instead? isn't x referring to the global variable x?
var x = 100;
x = x + 100;
outString += "from callMeoften2: " + "x = " + x + "<br/>";
}
答案 0 :(得分:0)
以下是通过变量提升的所有Javascript引擎如何与您的代码一起播放(有效)
function callMeOften2() {
var x = undefined;
outString += x + "<br/>"; //why isn't this going to give an output of 0? but gave an output of undefined instead? isn't x referring to the global variable x?
x = 100;
x = x + 100;
outString += "from callMeoften2: " + "x = " + x + "<br/>";
}
希望这有帮助
函数中的var x
表示全局x
无关紧要(当然可以在浏览器中使用window.x
访问,其他一些&#34; global&#34;对象在其他环境中)