【发布时间】:2012-10-04 03:35:57
【问题描述】:
我有下面的代码来定义一个类
var class1 = function () {
this.classData = 'value1';
this.func1 = function(callback) {
$.ajax({
'url': '/somewhere',
'dataType': 'json',
'type': 'POST',
'data': {
options: 'some text'
},
'success': function (data, textStatus, jqXHR) {
callback(data); // <<<<<< THIS LINE
}
});
};
};
然后我这样调用类
var obj1 = new class1();
obj1.func1(function (d) {
this.classData = d;
});
但这似乎不起作用,因为在 sucess 函数内部,当在上面代码中标记的行调用回调函数时,它的 this 对象指向 window 而不是 @ 987654325@值。
我在这里做错了什么,我该如何解决?
【问题讨论】:
标签: javascript jquery oop asynchronous scope