phpjayx Posted January 3, 2013 Share Posted January 3, 2013 function parseDataJson2(returnValue) //NEW REQ 3 { var listData2 = $.parseJSON(returnValue); var cellHtml = ''; for (var i = 0; i < listData2.length; i++) { var dataItem2 = listData2; } } I have the variable dataItem2 that I would like to be accessable from another function outside of this one. How do I make this a global variable? Or if there is an alternate way to get the data, that would be great too.. Thanks in advance Link to comment https://forums.phpfreaks.com/topic/272640-how-can-i-create-a-global-variable-from-inside-one-function-for-another-function-to-use/ Share on other sites More sharing options...
trq Posted January 3, 2013 Share Posted January 3, 2013 Or if there is an alternate way to get the data, that would be great too.. Return it from the function. Link to comment https://forums.phpfreaks.com/topic/272640-how-can-i-create-a-global-variable-from-inside-one-function-for-another-function-to-use/#findComment-1402949 Share on other sites More sharing options...
codefossa Posted January 3, 2013 Share Posted January 3, 2013 (edited) /* * Method 1: Declare the variable outside of the function. */ var myVar; function myFunc() { myVar = "This is a string"; } alert(myVar); /* * Method 2: Return data from the function. */ function myFunc() { return "This is a string"; } var myVar = myFunc(); alert(myVar); // You could also: alert(myFunc()); Edited January 3, 2013 by Xaotique Link to comment https://forums.phpfreaks.com/topic/272640-how-can-i-create-a-global-variable-from-inside-one-function-for-another-function-to-use/#findComment-1403015 Share on other sites More sharing options...
trq Posted January 3, 2013 Share Posted January 3, 2013 Please, don't ever do Method #1. Functions should not have side effects. Link to comment https://forums.phpfreaks.com/topic/272640-how-can-i-create-a-global-variable-from-inside-one-function-for-another-function-to-use/#findComment-1403071 Share on other sites More sharing options...
448191 Posted May 24, 2013 Share Posted May 24, 2013 While I agree that global functions should not have side effects, ever, "global" is key in this advice. After all, in JavaScript functions are objects, and a constructor or member functions manipulate the state of the object. OO is about state and your can't change state without side effects. Bit of a detour but an important distinction IMO. Side effects good, ignoring encapsulation bad. To put it simply. Link to comment https://forums.phpfreaks.com/topic/272640-how-can-i-create-a-global-variable-from-inside-one-function-for-another-function-to-use/#findComment-1432157 Share on other sites More sharing options...
.josh Posted May 25, 2013 Share Posted May 25, 2013 You can also use window.myVar Link to comment https://forums.phpfreaks.com/topic/272640-how-can-i-create-a-global-variable-from-inside-one-function-for-another-function-to-use/#findComment-1432223 Share on other sites More sharing options...
448191 Posted May 25, 2013 Share Posted May 25, 2013 You could, but you shouldn't, for similar reasons you don't use globals in PHP. Link to comment https://forums.phpfreaks.com/topic/272640-how-can-i-create-a-global-variable-from-inside-one-function-for-another-function-to-use/#findComment-1432284 Share on other sites More sharing options...
.josh Posted May 25, 2013 Share Posted May 25, 2013 Question was what can he do, not what should he do. I figured I'd just be repeating the latter. Link to comment https://forums.phpfreaks.com/topic/272640-how-can-i-create-a-global-variable-from-inside-one-function-for-another-function-to-use/#findComment-1432292 Share on other sites More sharing options...
448191 Posted May 25, 2013 Share Posted May 25, 2013 Guess I prefer giving advice over answering questions. Link to comment https://forums.phpfreaks.com/topic/272640-how-can-i-create-a-global-variable-from-inside-one-function-for-another-function-to-use/#findComment-1432301 Share on other sites More sharing options...
.josh Posted May 25, 2013 Share Posted May 25, 2013 I prefer both, and like I said, the advice was already covered. Link to comment https://forums.phpfreaks.com/topic/272640-how-can-i-create-a-global-variable-from-inside-one-function-for-another-function-to-use/#findComment-1432302 Share on other sites More sharing options...
448191 Posted May 25, 2013 Share Posted May 25, 2013 Fair enough, though I feel it's a bit irresponsible to provide alternatives that are ill-advised without explicit disclosure of that property. Link to comment https://forums.phpfreaks.com/topic/272640-how-can-i-create-a-global-variable-from-inside-one-function-for-another-function-to-use/#findComment-1432303 Share on other sites More sharing options...
.josh Posted May 25, 2013 Share Posted May 25, 2013 You mean like how you and thorpe already did? Link to comment https://forums.phpfreaks.com/topic/272640-how-can-i-create-a-global-variable-from-inside-one-function-for-another-function-to-use/#findComment-1432306 Share on other sites More sharing options...
448191 Posted May 26, 2013 Share Posted May 26, 2013 Uhuh, but you didn't reference any of that. Maybe it should've been obvious but I prefer being explicit. In any case let's leave it at this, we're going way off topic. Cool? Link to comment https://forums.phpfreaks.com/topic/272640-how-can-i-create-a-global-variable-from-inside-one-function-for-another-function-to-use/#findComment-1432308 Share on other sites More sharing options...
.josh Posted May 26, 2013 Share Posted May 26, 2013 I prefer not to assume the OP is a moron who needs something repeated to him literally inches below the last time. But that's just me. But you are right, we are going way off topic, so let's drop it. Link to comment https://forums.phpfreaks.com/topic/272640-how-can-i-create-a-global-variable-from-inside-one-function-for-another-function-to-use/#findComment-1432309 Share on other sites More sharing options...
448191 Posted May 26, 2013 Share Posted May 26, 2013 Cool. Link to comment https://forums.phpfreaks.com/topic/272640-how-can-i-create-a-global-variable-from-inside-one-function-for-another-function-to-use/#findComment-1432312 Share on other sites More sharing options...
Recommended Posts