javascript 将全局变量传递给函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18178728/
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-10-27 10:59:02  来源:igfitidea点击:

Passing a global variable to a function

javascriptscope

提问by Emanegux

How come the following code is giving me a 0 instead of a 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.

为什么下面的代码给我一个 0 而不是 1?我希望我的函数更改在函数外声明的变量,但我不想在函数声明中指定该变量。

that = 0;

function go(input) {
    input++;
}

go(that);

console.log(that);

采纳答案by Phil-R

As answered by Oriol, it doesn't work because the variable is passed by value, so you're not changing the "that" variable. A workaround would be to pass the variable name :

正如 Oriol 所回答的那样,它不起作用,因为变量是按值传递的,因此您没有更改“那个”变量。一种解决方法是传递变量名称:

that = 0;

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

test("that");

console.log(that); // 1

回答by Oriol

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'
    
  • 按值传递的变量

    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
    
  • 通过(值但用作)引用传递的变量

    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.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
    
  • 使您的变量成为对象的属性(在您的情况下,因为它是全局变量,请使用window)并传递对象(引用),以便您可以更改它

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

    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

  • 将变量转换为对象

    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
    

    那是因为对象也是按值传递的,但它们是一个引用。这意味着在函数内部你可以改变外部对象的属性(因为它是一个引用)但你不能改变整个对象,因为它是按值传递的。

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

回答by Matt Pavelle

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 thatan object which is by definition passed as a reference in Javascript. Then we just make sure we properly modify the object's attributes.

我们所做的全部是由一个对象,它是通过定义在JavaScript中引用传递。然后我们只需确保我们正确修改了对象的属性。

回答by woofmeow

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

回答by Nikos Spiropoulos

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

如果有人能解释为什么我错了,我会很高兴