【发布时间】:2010-05-11 05:41:50
【问题描述】:
我从一个 javascript 对象内部进行了一些 Ajax 调用。:
myObject.prototye = {
ajax: function() {
this.foo = 1;
var req = new XMLHttpRequest();
req.open('GET', url, true);
req.onreadystatechange = function (aEvt) {
if (req.readyState == 4) {
if(req.status == 200) {
alert(this.foo); // reference to this is lost
}
}
}
};
在 onreadystatechange 函数内部,this 不再引用主对象,所以我无权访问 this.foo。我可以在 XMLHttpRequest 事件中保留对主要对象的引用吗?
【问题讨论】:
标签: javascript ajax