首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >变量的LIfetime

变量的LIfetime
EN

Stack Overflow用户
提问于 2015-10-06 23:54:21
回答 1查看 452关注 0票数 0

我在理解下面的全局变量x的生命周期时遇到了一些困难。我对我的问题作了评论,我不理解。请帮帮我..。谢斯

代码语言:javascript
复制
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/>";
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-10-07 00:01:30

下面是所有Javascript引擎如何通过变量提升来处理代码(实际上)

代码语言: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访问,在其他环境中使用其他“全局”对象)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32981618

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档