我创建了以下类:
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 的那个)的引用已经丢失。我怎样才能把它拿回来?this
我知道我可以像这样在 AJAX 回调中传递一个 ref :
$.ajax ( { url: 'test.html' } ).done ( function ( source ) {
self.build ( source, self );
} );
但我真的不喜欢它,因为我觉得一个方法永远不应该将 ref 松散到它的对象上。
有什么想法/建议吗?:)