Javascript 方法外的可变内容
我正在从一个页面获取jsonp信息,我想对这些信息运行各种函数。信息很好地返回,但我似乎找不到一种方法使其在函数之外可访问。我知道这与闭包和函数范围有关,但我不知道如何使它工作,有什么想法吗 我可以通过多次调用json文件来实现脚本其余部分的目标,但我认为最好只查询json一次,然后将其弹出到变量中,然后尝试解决这个问题?我对这个设置比较陌生,所以任何建议都很感激Javascript 方法外的可变内容,javascript,json,jsonp,Javascript,Json,Jsonp,我正在从一个页面获取jsonp信息,我想对这些信息运行各种函数。信息很好地返回,但我似乎找不到一种方法使其在函数之外可访问。我知道这与闭包和函数范围有关,但我不知道如何使它工作,有什么想法吗 我可以通过多次调用json文件来实现脚本其余部分的目标,但我认为最好只查询json一次,然后将其弹出到变量中,然后尝试解决这个问题?我对这个设置比较陌生,所以任何建议都很感激 var AppInfo = { getData : function(){ var re
var AppInfo = {
getData : function(){
var responseJsonVar;
var callbackName, script, newInfo, mydata,allMatches;
// Get a random name for the callback
callbackName = "checkGames" + new Date().getTime() + Math.floor(Math.random() * 10000);
// create the jsonP script call on the page
script = document.createElement('script');
script.src = "http://www.hookhockey.com/index.php/temp-gillian/?callback=" + callbackName;
document.documentElement.appendChild(script);
// call the json
window[callbackName] = function(data) {
responseJsonVar = data; // this is the info back from the json file
//the filtered data source from json
var allMatches = responseJsonVar["matches"];
console.dir('allMatches inside the function: ' + allMatches); //this comes back fine
// Remove our callback ('delete' with 'window properties fails on some versions of IE, so we fall back to setting the property to 'undefined' if that happens)
try {
delete window[callbackName];
}
catch (e) {
window[callbackName] = undefined;
}
//I've tried putting a return value (return allMatches) in here and then calling window[callbackName]() outside of the function but I get undefined for matches
}; // end window[callbackName] function
//this is what I think I should be doing to get the info out on its own
console.dir('allMatches OUTSIDE the function: ' + allMatches); //this doesn't come back 'allMatches is not defined'
} //end getdata method
} //end AppInfo
AppInfo.getData();
通过下面的代码,我希望能够在getData方法运行后获得可在其外部访问的allMatches变量
感谢您的时间,非常感谢您的帮助
var AppInfo = {
getData : function(){
var responseJsonVar;
var callbackName, script, newInfo, mydata,allMatches;
// Get a random name for the callback
callbackName = "checkGames" + new Date().getTime() + Math.floor(Math.random() * 10000);
// create the jsonP script call on the page
script = document.createElement('script');
script.src = "http://www.hookhockey.com/index.php/temp-gillian/?callback=" + callbackName;
document.documentElement.appendChild(script);
// call the json
window[callbackName] = function(data) {
responseJsonVar = data; // this is the info back from the json file
//the filtered data source from json
var allMatches = responseJsonVar["matches"];
console.dir('allMatches inside the function: ' + allMatches); //this comes back fine
// Remove our callback ('delete' with 'window properties fails on some versions of IE, so we fall back to setting the property to 'undefined' if that happens)
try {
delete window[callbackName];
}
catch (e) {
window[callbackName] = undefined;
}
//I've tried putting a return value (return allMatches) in here and then calling window[callbackName]() outside of the function but I get undefined for matches
}; // end window[callbackName] function
//this is what I think I should be doing to get the info out on its own
console.dir('allMatches OUTSIDE the function: ' + allMatches); //this doesn't come back 'allMatches is not defined'
} //end getdata method
} //end AppInfo
AppInfo.getData();
获取所需内容的一个简单方法是将
allMatches
指定为窗口的属性,使其在任何地方都可以访问。替换
var allMatches = responseJsonVar["matches"];
与
然后稍后使用窗口访问它。allMatches您可以在AppInfo
对象上创建一个名为allMatches
的属性,并在数据从jsonp调用返回时设置该属性:
var AppInfo = {
allMatches: null, // NEW PROPERTY TO HOLD RETURNED DATA
confirmDataAvailableOutsideFunction: function () { // NEW FUNCTION TO VERIFY DATA AVAILABLE OUTSIDE getData()
console.dir('AppInfo.allMatches OUTSIDE the function AFTER jsonp call executes: ' + AppInfo.allMatches); //this doesn't come back 'allMatches is not defined'
},
getData: function () {
var responseJsonVar;
var callbackName, script, newInfo, mydata, allMatches;
// Get a random name for the callback
callbackName = "checkGames" + new Date().getTime() + Math.floor(Math.random() * 10000);
// create the jsonP script call on the page
script = document.createElement('script');
script.src = "http://www.hookhockey.com/index.php/temp-gillian/?callback=" + callbackName;
document.documentElement.appendChild(script);
// call the json
window[callbackName] = function (data) {
responseJsonVar = data; // this is the info back from the json file
//the filtered data source from json
AppInfo.allMatches = responseJsonVar["matches"]; // store data in allMatches property
console.dir('allMatches inside the function: ' + AppInfo.allMatches); //this comes back fine
AppInfo.confirmDataAvailableOutsideFunction(); // call test method to verify allMatches property is set
// Remove our callback ('delete' with 'window properties fails on some versions of IE, so we fall back to setting the property to 'undefined' if that happens)
try {
delete window[callbackName];
}
catch (e) {
window[callbackName] = undefined;
}
//I've tried putting a return value (return allMatches) in here and then calling window[callbackName]() outside of the function but I get undefined for matches
}; // end window[callbackName] function
//this is what I think I should be doing to get the info out on its own
console.dir('AppInfo.allMatches OUTSIDE the function BEFORE jsonp call executes: ' + AppInfo.allMatches); //this doesn't come back 'allMatches is not defined'
} //end getdata method
}; //end AppInfo
AppInfo.getData();
请注意,我修改了第二个console.dir
的文本,以指示它在jsonp调用返回之前运行,因此allMatches
属性此时仍然为null
这就是为什么在执行@peter-b关于使用window.allMatches
而不是局部变量allMatches
的建议后,window.allMatches
函数外部是未定义的
——您在设置之前检查了它
@peter-b的解决方案可以很好地工作,前提是您在设置之前没有尝试访问窗口.allMatches
。所以如果你想把数据存储在一个全局变量中,你可以使用他的方法;如果您希望将其存储在AppInfo对象中,可以使用我的
或者,您可以将所有内容包装在一个立即函数中,该函数将allMatches
作为局部变量:
(function () {
var allMatches = null;
var AppInfo = {
getData: function (dataReadyCallback) {
/* ... */
allMatches = responseJsonVar["matches"];
dataReadyCallback();
/* ... */
}
};
AppInfo.getData(allMatchesReady);
function allMatchesReady() {
console.dir('allMatches OUTSIDE the function AFTER jsonp call executes: ' + allMatches);
}
}());
嘿,谢谢你的建议。。我已按照建议进行了替换,然后将控制台日志更改为console.log(“函数外部的所有匹配项:”+window.allMatches);但我担心现在控制台中会出现“函数外的所有匹配:未定义”。