Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing a global variable to a function

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.

that = 0;

function go(input) {
    input++;
}

go(that);

console.log(that);
like image 750
Emanegux Avatar asked Aug 12 '13 02:08

Emanegux


3 Answers

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 :

that = 0;

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

test("that");

console.log(that); // 1
like image 184
Phil-R Avatar answered Nov 14 '22 05:11

Phil-R


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

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

like image 19
Oriol Avatar answered Nov 14 '22 05:11

Oriol


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:

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. Then we just make sure we properly modify the object's attributes.

like image 3
Matt Pavelle Avatar answered Nov 14 '22 03:11

Matt Pavelle