从javascript对象返回值

我创建了一个javascript对象,我试图从中获取som数据。 我使用jquery ajax来获取发送一些值,然后从PHP脚本中撤回它们。 这是有效的,但当我尝试使用我的javascript对象的instanse显示值时,我得到’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 

也许你可以使用回调来解决你的异步问题: http : //recurial.com/programming/understanding-callback-functions-in-javascript/