Declare an Actual Parameter as a Global Variable?

Discussion in 'Server and Client Scripting' started by Lutra, Jun 30, 2010.

  1. Lutra

    Lutra All Gen Battler/Scripter

    Joined:
    Apr 25, 2010
    Messages:
    509
    Likes Received:
    188
    PO Trainer Name:
    Lutra
    Is there a way to declare an actual parameter of a function as a global variable? I'm looking for a way to do a function that checks if a global variable exists and then declares it if it doesn't.
     
  2. Astruvis

    Astruvis Banned

    Joined:
    May 14, 2010
    Messages:
    217
    Likes Received:
    0
    uh, yes

    declare : fuction(name,val) {
    eval('if (typeof '+name+' == undefined) { ' +name+' = ' + val + '; return(false); }');
    return(true); }
     
  3. Lutra

    Lutra All Gen Battler/Scripter

    Joined:
    Apr 25, 2010
    Messages:
    509
    Likes Received:
    188
    PO Trainer Name:
    Lutra
    Ok thanks, I got it working with this:

    setvariable: function(variable,data){
    eval("if (typeof(" + variable + ") == 'undefined'){" + variable + "= data;}");
    }

    I think you forgot the brackets for typeof and that val needs to stay as a string. Oh and also you spelt function wrong.
     
  4. Astruvis

    Astruvis Banned

    Joined:
    May 14, 2010
    Messages:
    217
    Likes Received:
    0
    Eh, but I thought you would assign a numeral to it.

    Typeof does not need brackets.
     
  5. Lutra

    Lutra All Gen Battler/Scripter

    Joined:
    Apr 25, 2010
    Messages:
    509
    Likes Received:
    188
    PO Trainer Name:
    Lutra
    Typeof does need brackets. Otherwise it joins with the variable name.
     
  6. Akusu

    Akusu Guest

    typeOf is a function and not a keyword in Javascript, therefore you have to use brackets.

    nvm. Apparently it is a keyword hah.
     
    Last edited by a moderator: Jun 30, 2010
  7. Lutra

    Lutra All Gen Battler/Scripter

    Joined:
    Apr 25, 2010
    Messages:
    509
    Likes Received:
    188
    PO Trainer Name:
    Lutra