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