I have a JSON object that is returned inside a function (see below) and that in turn is inside a parent function and then a global function. There is a further child function within that global function that needs access to the JSON to parse the records.
I have left out some things that are not really part of the problem but hopefully included enough to explain the problem I have! You can see that
MyFunc.prototype.CreateMine = function(){
//do some work...
submit: function(e,v,m,f){
var xhr = new jsonRequest()
xhr.onreadystatechange = function(){
var jsonText = JSON.parse(xhr.responseText); //jsonText now contains my data
};
}
//need to access jsonText...
}
}
Let me know if the example of not complete enough, thanks in advance Chris
Probably you should learn more about how scopes in JavaScript work. To put it simple, all variables created in the outer functions are available in the inner ones. So you just have to declare your
jsonTextin one of the “upper” functions.Notice the absence of
varinsubmit: this way it will try to findjsonTextvariable upper in the scope tree:However, in your case a callback or a deferred would be better, because in
submitjsonTextis changed asynchronously, so you can’t access it right after invokingsubmit.