当我使用回调从异步调用返回时,我无法访问类的构造函数。
在这种情况下,我无法访问 Myclass1 构造函数中定义的测试变量
我找不到解决方案,我做错了什么?
var MyClass1 = function () {
this.test = 5;
};
MyClass1.prototype = function () {
//This is the function which calls another Myclass2 which contains the function for ajax //request
test = function () {
myClass2 = new MyClass2();
myClass2.getRequest.call(this, callback);
},
//This is the call Back
callback = function () {
alert(this.test); /*<---Cannot access this,shows undefined.In firebug it show this.test is not defined*/
};
return {
test: test,
callback: callback
}
}
MyClass2.prototype = function () {
getRequest = function () {
//make an async call set the success to the callback .I have propagated the data to //myclass1
$.ajax({
url: "test.html",
success: callBack.call(this, data) //<--I call the callback using myClass1
})
}
return {
getRequest: getRequest;
}
}