简体   繁体   English

将全局变量传递给函数

[英]Passing a global variable to a function

How come the following code is giving me a 0 instead of a 1?为什么下面的代码给我一个 0 而不是 1? I want my function to change a variable declared outside the function but I do not want to specify the variable in the function declaration.我希望我的函数更改在函数外声明的变量,但我不想在函数声明中指定该变量。

that = 0;

function go(input) {
    input++;
}

go(that);

console.log(that);

As answered by Oriol, it doesn't work because the variable is passed by value, so you're not changing the "that" variable.正如 Oriol 所回答的那样,它不起作用,因为变量是按值传递的,因此您没有更改“那个”变量。 A workaround would be to pass the variable name :一种解决方法是传递变量名称:

that = 0;

function test(input) {
    window[input]++;
}

test("that");

console.log(that); // 1

That's because you are passing the variable by value, not by reference.那是因为您是按值传递变量,而不是按引用传递。

In javascript, all variables are passed by value, except objects, which are passed by reference (well, in fact they are passed by value too but they are a reference, see below).在javascript中,所有变量都是按值传递的,除了对象,它们是按引用传递的(好吧,实际上它们也是按值传递的,但它们是一个引用,见下文)。

And you can't change that behaviour.你无法改变这种行为。

Edit: If you don't know what passing by value/reference means, you should read a tutorial.编辑:如果您不知道按值/引用传递是什么意思,您应该阅读教程。 But here you have some examples:但这里有一些例子:

  • Variable passed by value按值传递的变量

    function foo(bar){ console.log(bar); // 1 bar++; console.log(bar); // 2 } var mybar = 1; console.log(mybar); // 1 foo(mybar); console.log(mybar); // 1
  • Variable passed by (value but used as a) reference通过(值但用作)引用传递的变量

    function foo(bar){ console.log(bar.a); // 'b' bar.a = 'c'; console.log(bar.a); // 'c' } var mybar = {a:'b'}; console.log(mybar.a); // 'b' foo(mybar); console.log(mybar.a); // 'c'

In your case在你的情况下

You can do你可以做

  • Make your variable a property of an object (in your case, since it's a global variable, use window ) and pass the object (reference), so you can alter it使您的变量成为对象的属性(在您的情况下,因为它是全局变量,请使用window )并传递对象(引用),以便您可以更改它

    window.that = 0; function go(obj) { obj.that++; } go(window); console.log(that); // 1
  • Use a return value使用返回值

    var that = 0; function go(input) { return input++; } that = go(that); console.log(that); // 1

Note that you can't do请注意,您不能这样做

  • Convert your variable into an object将变量转换为对象

    var that = new Number(0); // Now it's an object number function go(input) { input++; } go(that); that *= 1; // Now it's a literal number console.log(that); // 0

    That's because objects are passed by value too, but they are a reference.那是因为对象也是按值传递的,但它们是一个引用。 That means that inside the function you can change the properties of the outer object (because it's a reference) but you can't change the entire object, because it's passed by value.这意味着在函数内部您可以更改外部对象的属性(因为它是一个引用)但您不能更改整个对象,因为它是按值传递的。

    See examples here: https://stackoverflow.com/a/3638034/1529630请参阅此处的示例: https : //stackoverflow.com/a/3638034/1529630

This has to do with pointers, scope, passing variables by reference, and all that jazz.这与指针、作用域、通过引用传递变量以及所有爵士乐有关。

If you really want to do this, you can pass an object in Javascript like this:如果你真的想这样做,你可以像这样在 Javascript 中传递一个对象:

var that = {value: 0};
function go(input) {
    input.value++;
}
go(that);
console.log(that.value);

All we've done is made that an object which is by definition passed as a reference in Javascript.所有我们所做的就是作出一个对象,它是通过定义在JavaScript中引用传递。 Then we just make sure we properly modify the object's attributes.然后我们只需确保我们正确修改了对象的属性。

Your code你的代码

that = 0; //Global variable

function go(input) { //input is argument and is not passed by reference
    input++; //This just increments a local copy i.e 0
}

go(that); //Passed 0 

console.log(that);

Instead do this而是这样做

that = 0;

function go() {
    that++;
}

go(); //Not passing any variable .. function can already see the gloabl "that" 

console.log(that); // This will print gloabl i.e. 1

Actually you could just add console.log(input) inside the function and it would work just fine.实际上你可以在函数中添加console.log(input) ,它会工作得很好。

Please correct me if i'm wrong.如果我错了,请纠正我。 Hope i helped !!希望我有所帮助!!

I would be glad if somebody could explain why im wrong如果有人能解释为什么我错了,我会很高兴

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

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