我在理解下面的全局变量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/>";
}发布于 2015-10-07 00:01:30
下面是所有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访问,在其他环境中使用其他“全局”对象)。
https://stackoverflow.com/questions/32981618
复制相似问题