Passing local variables from a function out to become global variables
15,039
Solution 1
The clean way to do this is using js-object notation:
function showTest(str) {
//other code
return {arr: arrayvals, tm: arrtime};
}
var func_result = showTest("blah-blah");
var testvar =func_result.tm;
var testvar2=func_result.arr;
But it's generally a bad idea to have global vars. Why do you need it?
Update sample code with global
object
globals = {};
function q(){
globals['a'] = 123;
globals[123] = 'qweqwe';
}
function w(){
alert(globals.a);
//alert(globals.123); //will not work
alert(globals[123]); //that's OK.
}
q();
w();
Solution 2
You can declare the variables outside of the function.
var arrtime, arrayvals;
function showTest(str) {
arrayvals = JSON.parse(xmlhttp.responseText);
arrtime= (arrayvals[0]);
}
var testvar=arrtime;
alert (testvar);
Related videos on Youtube
Author by
Peter
Updated on June 26, 2022Comments
-
Peter almost 2 years
I've spent the last two hours trying to figure out how to do this but nothing is working. Here is a short sample of some of my code. I want to get arrtime and several other similar variables out of the function so I can use them globally. Any ideas? Nothing too complicated please, I'm no expert (obviously).
function showTest(str) { ........ var arrayvals = JSON.parse(xmlhttp.responseText); var arrtime= (arrayvals[0]); } var testvar=arrtime; document.getElementById("testing").innerHTML=testvar;
-
Peter over 12 yearsI need to use the variables in several other functions and I believe the easiest way to do this would be to make them global. Thanks for that above, I'll give it a try
-
J0HN over 12 yearsIt's the easiest way, but not the wisest (like turning to the
Dark Side of the force
:)). Introduce one global object and store those vars as it's properties. This way you'll be protected hiding global vars in local scopes with vars with the same name. Objects are instantiated asobj = {param: value, other_param: other_value}
. -
Peter over 12 yearsOK so would this work? I create dbresults= new Object(); where I have all my global variables declared at the start. Then in the function I say dbresults.arrtime= (arrayvals[0]); And then outside the function again I can say document.getElementById("testing").innerHTML=dbresults.arrtime; Would that work?
-
J0HN over 12 yearsNo. JS objects is not created as in C/PHP/JAva etc. It's just
{}
:). You don't have to set all vars at object declaration, you can add it later (search in SO, we have that question a day or two ago). So, you just doglobals = {}
; and then, anywhere you need you access it withglobals.param
. -
Peter over 12 yearsI'm not sure I'm following you correctly. I did this: dbresults={};function showTest(str) {...... dbresults.arrtime= (arrayvals[0]);document.getElementById("range").innerHTML=dbresults.arrtime; } document.getElementById("testing").innerHTML=dbresults.arrtime; So the part within the function succeeds in writing to the element I was testing it on, but the same thing outside the function gives me this error: 108Uncaught TypeError: Cannot set property 'innerHTML' of null. If you could show me what code to write or even a example for dumbies like me I'd appreciate it!
-
Peter over 12 yearsThank you so much! You've ended my hours of frustration! It works! :D