这是我问的另一个question。它在评论中得到了解决,所以我没有找到答案。我想知道变量范围:
var JOHNNY = (function()
{
var module = {};
function getData(id, callback){
var xhr = new XMLHttpRequest();
var url = "http://someurl/api/5";
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && (xhr.status == 200)) {
callback(xhr.response);
}
};
xhr.open("GET", url, true);
xhr.responseType = 'json';
xhr.send(null);
}
function myCallBack(data){
return data; //console.log works;
}
module.getStuff=function(id){
return getData(5, myCallBack); //just hardcoded id here
}
return module;
})();
我知道为什么由于异步通信而无法正常工作。所以,我明白了,
JOHNNY.getStuff(5);
undefined
同样,我知道为什么会这样。这不是我要问的部分(异步部分)。
我的问题与这部分有关:
如果我这样做,我可以得到数据,
function myCallBack(data){
module.test = data;
}
然后,
JOHNNY.test;
//has the answer, 5,; this assumes the callback has been called for the questions sake.
回调会将数据分配给回调中的变量module
,myCallBack
,我不会依赖即时回答,这是不可靠的,等等。
我没有得到的是私有命名空间中的 在控制台中我已经完成了我的电话, JOHNNY.getStuff(5); 已经在IIFE退回了module
变量如何在之后更新我调用了该函数并且已经创建了对JOHNNY的引用。< / p>
module
?变量module
不需要在这里使用原型来更新所有事件?