【发布时间】:2015-10-06 23:54:21
【问题描述】:
我在理解下面的全局变量 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/>";
}
【问题讨论】:
标签: javascript variables lifetime