Javascript change the value of the global variable

How to transfer the global variable as an actual parameter to the function to change the value of the global variable.

How to Change the Value of error

function fn_check_input(reg, obj, tips, error){
        var reg = reg;
        var val = obj.val();
        if (!val){
            error = true;
            obj.next().html(tips[0]).show();
            return;
        }

        if (reg.test(val)){
            obj.next().hide()
            error = false;
        }else{
            error = true;
            obj.next().html(tips[1]).show();
        }
    }

4 answers

  • answered 2018-07-11 03:18 CollinD

    I don't know that I fully understand your question as it relates to global variables, but I can tell you:

    Javascript is pass-by-value. Luckily, an object is just a reference, so when it is passed-by-value, you can mutate its members (since it still points to the same actual object).

    You can do something like this:

    // @param {Object} error - error.status will be modified by this function
    function mutateError(error) {
        error.status = false;
    }
    
    // error is no longer a primitive, so we can mutate it now
    var error = { status: true }
    
    console.log(error.status); // true
    
    mutateError(error);
    
    console.log(error.status); // false

    As others have mentioned, you probably don't really even want to do this, and would likely be better off by just return error;ing.

  • answered 2018-07-11 03:18 Christian Scillitoe

    Javascript uses pass-by-value, which means changing the value of error inside of this function is only changing it's value in the scope of this function.

    Perhaps consider having your function return the value that you want to store in error, and update the value of error in the calling function accordingly:

    function fn_check_input(reg, obj, tips /*Removed Error Variable*/){
        var reg = reg;
        var val = obj.val();
        if (!val){
            //error = true;
            obj.next().html(tips[0]).show();
            return true; //Return the value of error
        }
    
        if (reg.test(val)){
            obj.next().hide()
            //error = false;
            return false; //Return the value of error
        } else{
            //error = true;
            obj.next().html(tips[1]).show();
            return true; //Return the value of error
        }
    }
    

    Now when you call the function:

    error = fn_check_input(reg, obj, tips);
    

  • answered 2018-07-11 03:28 Wolf-Kun

    If you are using a global variable, don't worry about passing it as an argument.

    var Test=false;
    //uses global var
    function F1() {
        Test=true;
    }
    //Test is argument, so it is no longer global.
    function F2(Test){
    Test=true;
    }
    function Click(){
    F1();
    alert(Test); //true. Changes to global variable work as expected.
    Test=false; 
    F2(Test); 
    alert(Test)//false. the global variable is not changed.
    }
    

  • answered 2018-07-11 03:52 mannok

    In JS, if the variable is primitive type. You can pass the variable with its parent. For example:

    function func(parent){
        parent.child = 'foo';
    };