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

7
  • Check this question just in case, stackoverflow.com/questions/14220321/… Commented Jul 16, 2013 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. Commented Jul 16, 2013 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. Commented Jul 16, 2013 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. Commented Jul 16, 2013 at 21:17
  • happy writing enterprise/concurrent software with a lot of global variables. JavaScript by nature is callback/event based language. Commented Jul 16, 2013 at 21:21

3 Answers 3

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');
Sign up to request clarification or add additional context in comments.

5 Comments

16 seconds too slow :-P
@dandavis yeah but I have included the getBackList call. Just kidding.
tisk tisk, but by our powers combined... ;)
Thanks Ill accept this answer since it was first, but voted both up.
@dnak mine was first :-P But who is counting? Good job jods ^_^
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 ^^^

Comments

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

2 Comments

Thanks, I actually like this answer alot.
This is cleaner than passing strings for variable names

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.