如何访问函数外的 Javascript 变量值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10714069/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 02:38:28  来源:igfitidea点击:

How to access Javascript variable values outside of the function

javascriptarraysfunctionvariables

提问by GeordieDave1980

I've been banging my head off a brick wall with this one and another all nighter with no success. What I would like to do is have access to the values set in an array within a function but outside of that function. How could this be done? For example:

我一直在用这个一个又一个晚上把头撞到砖墙上,但没有成功。我想要做的是访问在函数内但在该函数外的数组中设置的值。这怎么可能?例如:

function profileloader()
{
    profile = [];
    profile[0] = "Joe";
    profile[1] = "Bloggs";
    profile[2] = "images/joeb/pic.jpg";
    profile[3] = "Web Site Manager";
}

I would then further down the page within a paragraph tag have something like:

然后,我会在段落标签中的页面下方有类似的内容:

document.write("Firstname is: " + profile[0]);

Obviously that would be contained with in the script tag but all i'm getting is an error on the console stating: "profile[0] is not defined".

显然,这将包含在脚本标记中,但我得到的只是控制台上的错误,指出:“未定义配置文件 [0]”。

Anyone got any ideas where I'm going wrong? I just can't seem to figure it out and none of the other solutions I've seen when passing values from either function to function or outside of a function, have worked so far.

有人知道我哪里出错了吗?我似乎无法弄清楚,到目前为止,我在从函数到函数或在函数外部传递值时所看到的其他解决方案都没有奏效。

Thank you to anyone who can help me with this, its probably something simple I've missed!

感谢任何可以帮助我解决这个问题的人,这可能是我错过的一些简单的事情!

回答by Joseph

declare it outside the function so the outside scope can see it (be careful of globals though)

在函数之外声明它,以便外部范围可以看到它(但要小心全局变量)

var profile = [];
function profileloader(){
    profile[0] = "Joe";
    profile[1] = "Bloggs";
    profile[2] = "images/joeb/pic.jpg";
    profile[3] = "Web Site Manager";
}

or have the function return it:

或者让函数返回它:

function profileloader(){
    var profile = [];
    profile[0] = "Joe";
    profile[1] = "Bloggs";
    profile[2] = "images/joeb/pic.jpg";
    profile[3] = "Web Site Manager";
    return profile;
}

var myprofile = profileloader(); //myprofile === profile

回答by mplungjan

Since you do NOT have a varin front of the profile=[];, it is stored in the global window scope.

由于var前面没有profile=[];,因此它存储在全局窗口范围内。

What I suspect is that you forgot to call the profileloader() before using it.

我怀疑您在使用之前忘记调用 profileloader() 。

It is good practice is to declare your global variables in an obvious manner, as shown in other answers on this page

好的做法是以明显的方式声明全局变量,如本页的其他答案所示

It is not considered good practice to rely on side effects.

依赖副作用不被认为是好的做法。



Commented code to show what is going on, NOTEnot recommended method:

注释代码以显示正在发生的事情,注意不推荐的方法:

This should work. And it does work: DEMO

这应该有效。它确实有效:DEMO

function profileloader()
{
    profile = []; // no "var" makes this global in scope
    profile[0] = "Joe";
    profile[1] = "Bloggs";
    profile[2] = "images/joeb/pic.jpg";
    profile[3] = "Web Site Manager";
}
profileloader(); // mandatory
document.write("Firstname is: " + profile[0]);