从javascript对象返回值 [英] Returning value from javascript object
本文介绍了从javascript对象返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我创建了一个javascript对象,并尝试从中获取som数据. 我使用jquery ajax来获取发送一些值,然后从php脚本重现它们. 这行得通,但是当我尝试使用我的javascript对象的实例显示该值时,我会得到未定义".
I have created a javascript object and im trying to fetch som data from it. Im using jquery ajax to fetch send some values and then returing them from a php script. this works, but when i try to use an instanse of my javascript object to dispaly the value i get 'undefined'.
对象:
var getDBresults = (function () {
function getResult(url,TableName ,callback){
$.ajax({
url: url,
type: 'POST',
data: {
'table':TableName,
},
success: function(data){
callback(data);
},
error: function(event){
alert(event.error);
}
});
}
return {
getAllVideoes: function(){
getResult("getAllResults.php", "videoer", function(data){
console.log(data); //Works
return data;
});
},
}
})();
在我的其他脚本中:
var obj = getDBresults;
var data = obj.getAllVideoes(); //this runs, and produce the log showed above
console.log(data) //results in undefined
console.log(obj.getAllVideoes()) //results in undefined
推荐答案
Maybe you can use callbacks it will solve your async problems: http://recurial.com/programming/understanding-callback-functions-in-javascript/
这篇关于从javascript对象返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文