0

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

dnak
  • 138
  • 1
  • 1
  • 9
  • Check this question just in case, http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call – elclanrs Jul 16 '13 at 20:59
  • Globals are great, and using one function you can call many times to set a lot of global variables is considered good practice. – adeneo Jul 16 '13 at 21:01
  • Global variables should not be used too much. It is considered anti-pattern. Global variables create state witch make software very tight coupled and hard to debug. – user1759572 Jul 16 '13 at 21:04
  • @user1759572 - nonsense, global variables are the best thing to happen to javascript ever, it's like sand in sahara, it should be everywhere. – adeneo Jul 16 '13 at 21:17
  • happy writing enterprise/concurrent software with a lot of global variables. JavaScript by nature is callback/event based language. – user1759572 Jul 16 '13 at 21:21
  • @adeneo I'm not sure if you're being serious or sarcastic. I'm am very new to JS and a lot of these concepts are new to me. – dnak Jul 16 '13 at 21:41
  • @user1759572 Yeah from what I've been reading it is bad practice to use too many global, I'm looking at different ways to approach the problem. – dnak Jul 16 '13 at 21:42

3 Answers3

4
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 ^^^

Naftali
  • 144,921
  • 39
  • 244
  • 303
4

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');
jods
  • 4,581
  • 16
  • 20
3

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, 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);
Steven10172
  • 2,003
  • 4
  • 21
  • 37