简体   繁体   English

将全局变量传递给函数-函数运行后全局变量不更新

[英]Passing Global Variable to Function - Global Variable Not Updating After Function Run

My code increments a number up or down by 1. The problem is "testNum" which is a global variable should reflect the increments up or down but when I check the value of "testNum" in the console it's always 20. I pass "testNum" to my function and return it. 我的代码将数字递增或递减1。问题是“ testNum”,这是一个全局变量,应反映递增或递减的增量,但是当我在控制台中检查“ testNum”的值时,它始终为20。我通过了“ testNum” ”并返回它。 Why isn't it reflecting the increments? 为什么它不反映增量?

Fiddle: http://jsfiddle.net/FnUXw/1/ 小提琴: http : //jsfiddle.net/FnUXw/1/

<input type="button" id="btnDown" value="-">
<span id="amountDiv">amount div</span>
<input type="button" id="btnUp" value="+">

<script>
var testNum = 20;

amountDiv.innerHTML = testNum;

function makeButtonIncrement(button, upDown, displayDiv, variable) {

    function changeValue () {
        if (upDown == "up") {
            variable++;
        }

        if (upDown == "down") {
            variable--;
        }

        displayDiv.innerHTML = variable;
    }

    button.onclick = changeValue;

    return variable;
}

makeButtonIncrement(document.getElementById('btnDown'), "down", document.getElementById('amountDiv'), testNum);

makeButtonIncrement(document.getElementById('btnUp'), "up", document.getElementById('amountDiv'), testNum);
</script>

Numbers are passed by value, not be reference so you can do what you're trying to do. 数字是通过值传递的,而不是作为参考传递的,因此您可以执行自己想做的事情。 With your code structured the way it is, you would need to do it like this where you assign the return value: 按照代码的原样进行结构化之后,您需要在分配返回值的过程中执行以下操作:

testNum = makeButtonIncrement(document.getElementById('btnDown'), "down", document.getElementById('amountDiv'), testNum);

Only arrays and objects are passed by reference so they can be passed as arguments and have the function modify the original variable. 只有数组和对象是通过引用传递的,因此它们可以作为参数传递,并且可以让函数修改原始变量。 Other types are passed by value (eg a copy is made for the argument) so you cannot modify the original. 其他类型按值传递(例如,为参数创建副本),因此您无法修改原始类型。

The work-around to modify the original is to pass it as a property of an object. 修改原始文件的方法是将其作为对象的属性传递。 Then, because the object is passed by reference, the function can modify the original property value. 然后,由于该对象是通过引用传递的,因此该函数可以修改原始属性值。


Or, since you're already using a global variable, you could just modify the global variable directly and not even pass it as an argument. 或者,由于您已经在使用全局变量,因此可以直接修改全局变量,甚至不将其作为参数传递。 It is the passing as an argument that makes a copy of it so you can't modify the original. 它是作为参数传递的副本,因此您无法修改原始参数。 Global variables are, in general, not recommended, but if it's already global for other reasons, you can just modify it directly in your function without passing it as an argument. 通常不建议使用全局变量,但是如果由于其他原因它已经是全局变量,则可以直接在函数中对其进行修改,而无需将其作为参数传递。

You are using "variable++" and "variable--" which is incrementing or decrementing that locally scoped variable (local to the function scope). 您正在使用“ variable ++”和“ variable--”,它们正在递增或递减该局部范围的变量(局部于函数范围)。 The parameter is not a pointer to the global variable, it is passed by value (not by reference). 该参数不是指向全局变量的指针,而是通过值(而不是通过引用)传递。

The simplest modification would be to change variable++ and variable-- to testNum++ and testNum-- respectively and remove that final function parameter as it is unnecessary. 最简单的修改是将variable++variable--分别更改为testNum++testNum--并删除该最终函数参数,因为这是不必要的。

function makeButtonIncrement(button, upDown, displayDiv) {

    function changeValue () {
        if (upDown == "up") {
            testNum++;
        }

        if (upDown == "down") {
            testNum--;
        }

        displayDiv.innerHTML = testNum;
    }

    button.onclick = changeValue;
}

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

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