简体   繁体   English

如何更改 function 内部的全局变量的值

[英]How do I change the value of a global variable inside of a function

I am using JavaScript and I create a global variable.我正在使用 JavaScript 并创建一个全局变量。 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?我在 function 之外定义它,我想从 function 内部更改全局变量值并从另一个 function 中使用它,我该怎么做?

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 .这就是为什么始终使用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 a = 10;

myFunction();

function myFunction(){
   a = 20;
}

alert("Value of 'a' outside the function " + a); //outputs 20

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 xa 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 又出现了两个关键字constlet ,它们也会影响变量的作用域( 语言规范

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.variableName you can modify the value of a global variable inside a function.使用window['variableName']window.variableName您可以修改函数内全局变量的值。

<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>

A simple way is to use var一个简单的方法是使用var

var apple = null;
const some_func =()=>{
      apple = 25
}
some_func()
console.log(apple)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM