Javascript 如何更改函数内全局变量的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10872006/
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
How do I change the value of a global variable inside of a function
提问by NullPoiиteя
I am using JavaScript and I create a global variable. I define it outside of a function and I want to change the global variable value from inside a function and use it from another function, how do I do this?
我正在使用 JavaScript 并创建了一个全局变量。我在函数外部定义它,我想从函数内部更改全局变量值并从另一个函数使用它,我该怎么做?
回答by Spudley
Just reference the variable inside the function; no magic, just use it's name. If it's been created globally, then you'll be updating the global variable.
只需引用函数内部的变量即可;没有魔法,只需使用它的名字。如果它是全局创建的,那么您将更新全局变量。
You can override this behaviour by declaring it locally using var
, but if you don't use var
, then a variable name used in a function will be global if that variable has been declared globally.
您可以通过使用 本地声明来覆盖此行为var
,但如果您不使用var
,则如果该变量已全局声明,则函数中使用的变量名称将是全局的。
That's why it's considered best practice to always declare your variables explicitly with var
. Because if you forget it, you can start messing with globals by accident. It's an easy mistake to make. But in your case, this turn around and becomes an easy answer to your question.
这就是为什么始终使用var
. 因为如果你忘记了它,你可能会不小心开始弄乱全局变量。这是一个容易犯的错误。但在你的情况下,这反过来成为你问题的简单答案。
回答by Chris
var a = 10;
myFunction();
function myFunction(){
a = 20;
}
alert("Value of 'a' outside the function " + a); //outputs 20
回答by yunzen
Just use the name of that variable.
只需使用该变量的名称。
In JavaScript, variables are only local to a function, if they are the function's parameter(s) or if you declare them as local explicitely by typing the var
keyword before the name of the variable.
在 JavaScript 中,变量只是函数的局部变量,如果它们是函数的参数,或者如果您通过var
在变量名称前键入关键字将它们显式声明为局部变量。
If the name of the local value has the same name as the global value, use the window
object
如果本地值的名称与全局值的名称相同,则使用window
对象
See this jsfiddle
看到这个jsfiddle
x = 1;
y = 2;
z = 3;
function a(y) {
// y is local to the function, because it is a function parameter
console.log('local y: should be 10:', y); // local y through function parameter
y = 3; // will only overwrite local y, not 'global' y
console.log('local y: should be 3:', y); // local y
// global value could be accessed by referencing through window object
console.log('global y: should be 2:', window.y) // global y, different from local y ()
var x; // makes x a local variable
x = 4; // only overwrites local x
console.log('local x: should be 4:', x); // local x
z = 5; // overwrites global z, because there is no local z
console.log('local z: should be 5:', z); // local z, same as global
console.log('global z: should be 5 5:', window.z, z) // global z, same as z, because z is not local
}
a(10);
console.log('global x: should be 1:', x); // global x
console.log('global y: should be 2:', y); // global y
console.log('global z: should be 5:', z); // global z, overwritten in function a
Edit
编辑
With ES2015 there came two more keywords const
and let
, which also affect the scope of a variable (Language Specification)
ES2015 增加了两个关键字const
and let
,它们也影响变量的作用域(语言规范)
回答by Iman Sedighi
<script>
var x = 2; //X is global and value is 2.
function myFunction()
{
x = 7; //x is local variable and value is 7.
}
myFunction();
alert(x); //x is gobal variable and the value is 7
</script>
回答by Sterling Diaz
var a = 10;
myFunction(a);
function myFunction(a){
window['a'] = 20; // or window.a
}
alert("Value of 'a' outside the function " + a); //outputs 20
With window['variableName']or window.variableNameyou can modify the value of a global variable inside a function.
使用 window['variableName']或window.variableName您可以修改函数内全局变量的值。