简体   繁体   中英

Assigning multiple global variables in JavaScript with one function

I've been looking around for a while and cant seem to find a solution to this question.

I have three global variables declared in JavaScript, which haven't been assigned yet such as:

var GLOBAL_VARIABLE_ONE;
var GLOBAL_VARIALLE_TWO;
var GLOBAL_VARIABLE_THREE;

Lets say I have a function which I pass a two parameters, one of them is a url string to make an ajax call to retrieve a JSON object, the other is the global variable which I want to assign the returned JSON to. Thus, I have a function as such:

function getBackList(urlName, globalVariable) {
    $.ajax({
    type: "GET",
    url: urlName,
    }).done(function (returned_data) {
        globalVariable = $.parseJSON(returned_data);
    });
}

I want to be able to call the function like:

getBackList("\some\url", GLOBAL_VARIABLE_ONE);

and assign the object to the global.

Now, I realize that using global variables are not recommended and I understand that due to variable hoisting, the arguments passed to a function are locally scoped. However, I need to be able to access these global variables in other functions after they have been assigned.

My question is how can I pass the global variables and assign them into the one function above, without having to create separate functions to assign each one explicitly (which works by the way but creates a lot of code redundancy)?

function getBackList(urlName, globalVariableName) {
    $.ajax({
       type: "GET",
       url: urlName,
    }).done(function (returned_data) {
        window[globalVariableName] = $.parseJSON(returned_data);
    });
}

Pass in the global variable's name instead ^^^

global variables are actually members of the window object. So you could do:

function getBackList(x, variableName) {
  // .. stuff ..
  window[variableName] = $.parseJSON(returned_data);
}

getBackList(x, 'GLOBAL_VARIABLE_ONE');

Javascript won't allow that as it's passing the variable.

If GLOBAL_VARIABLE_ONE is an object it'll pass a reference.

Examples:

a = {}

a now references the new object.

b = a;

b now references the same object that a references. Note that it does not reference a .

With a = {}; b = a a = {}; b = a , you get

a
 \
  \
   { }
  /
 /
b

Then with a['one'] = {} you get

a
 \
  \
   { one: { } }
  /
 /
b

In order to achieve what you want you'll want GLOBAL_VARIABLE_ONE to be an object.

So you can do:

var GLOBAL_OBJECT = {};
getBackList("\some\url", GLOBAL_OBJECT['GLOBAL_VARIABLE_ONE']);

Or you can do:

var GLOBAL_VARIABLE_ONE = {}
var GLOBAL_VARIALLE_TWO = {}
var GLOBAL_VARIABLE_THREE = {}

function getBackList(urlName, globalVariable) {
    $.ajax({
    type: "GET",
    url: urlName,
    }).done(function (returned_data) {
        globalVariable.json = $.parseJSON(returned_data);
    });
}

getBackList("\some\url", GLOBAL_VARIABLE_ONE);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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