我创建了以下类:jQuery的回调和原型继承
APP.core.View = function() {
var self = this;
$.ajax ({ url: 'test.html' }).done (self.build);
return self;
};
APP.core.View.prototype.build = function (source) {
var self = this;
// this refers to the AJAX callback.
return self;
};
正如你可以在build
方法看,this
参考(一个属于APP.core.View)已丢失。我怎样才能找回来?我知道我可以在AJAX回调像这样的裁判传给this
:
$.ajax ({ url: 'test.html' }).done (function (source) {
self.build (source, self);
});
但我真的不喜欢它,因为我觉得自己像一个方法应该永远不会失去裁判给它的对象。
任何想法/建议? :)