1

I am trying to set a global variable via function call based on the specified options/values of the function call. Here's my code:

    let g_Pl = [];

    function prepare() {
        let s = 0;

            s = 1;
            g_Pl[s] = 5;

            s = 2;
            g_Pl[s] = 8;

            s = 3;
            g_Pl[s] = 10;
        }

    function getInfo(s,map,pl) {
        switch (map) {
            case "test":
                pl = g_Pl[s];
            break;
        }
    }

function test() {
    let local_Pl;

    getInfo(1, "test", local_Pl)

    console.log(local_Pl);
}

prepare();
test();

But the console output is "undefined" and I am wondering why? local_Pl is supposed to be set a value from getInfo which has to be "5" based on the parameters in prepare():

s = 1;
g_Pl[s] = 5;

Why it doesn't work ?

Jakub Moz
  • 127
  • 10
  • You are never setting `local_Pl`. You only initialised it using `let local_Pl;` but never assigned it a value. – Wais Kamal Feb 14 '19 at 10:52
  • Well javascript is a little bit confusing for me coming from VB.NET – Jakub Moz Feb 14 '19 at 11:12
  • Possible duplicate of [How best to implement out params in JavaScript?](https://stackoverflow.com/questions/3175687/how-best-to-implement-out-params-in-javascript) – nick zoum Feb 14 '19 at 11:31

1 Answers1

1

You are using pl and local_Pl as an out parameter aka pass by reference parameter or ByRef, but JavaScript doesn't support that functionality. You should instead return the result, like so:

function getInfo(s, map) {
    switch (map) {
        case "test":
            return g_Pl[s];
    }
}

function test() {
    let local_Pl = getInfo(1, "test");
    console.log(local_Pl);
}

If you need to return something and also have an out parameter then you can just create an object to contain both and return that object.

function getInfo(s, map) {
    var element;
    switch (map) {
        case "test":
            element = g_Pl[s];
            break;
    }
    return { found: !!element, pl: element };
}

function test() {
    let result = getInfo(1, "test");
    if (result.found) console.log(result.pl);
}
nick zoum
  • 7,216
  • 7
  • 36
  • 80
  • Thanks for the answer. It works, but what if I want to return 2 values (ex: g_Pl[s] and g_R[s]) from the getInfo function. How do I then assign g_Pl[s] to local_Pl and g_R[s] to different variable ? – Jakub Moz Feb 14 '19 at 11:11
  • Actually I just did return g_Pl[s] + "," + g_R[s]; then let localVar = getInfo(etc,etc); then let strSplit = localVar.split(","); and then simply let anotherVar = strSplit[0]; It seems easier to me I don't know the "performance" of my solution, so if you can tell me if it's either ok or not it will be nice. Again thanks for the answer it pointed me to the right direction. – Jakub Moz Feb 14 '19 at 11:53
  • If my answer helped you, you can up-vote it, but I would still strongly recommend using objects over `concat` and `split` – nick zoum Feb 14 '19 at 13:14
  • I marked your question as solution sorry I forgot to up vote it. – Jakub Moz Feb 15 '19 at 07:57