如何为jQuery回调绑定"this"?

Sam*_*Lee 6 ajax jquery

我试图在jQuery中设置一个回调,正确地将"this"绑定到调用对象.具体来说,这是我正在使用的代码.我在这样的对象中进行ajax调用:

Object.prototype.doAjaxCall = function(url)
    {
        $.get(url, null, this.handleAjaxResponse );
    }
Run Code Online (Sandbox Code Playgroud)

但是,在Object.prototype.doAjaxCall,this并没有提到正确的事情.我之前使用过Prototype,我知道你需要在执行Ajax调用时绑定它,但我似乎无法在jQuery中找到正确的方法.有人能指出我正确的方向吗?

Joe*_*ung 1

ECMAScript 3.1 中应该包含更强大的本机绑定函数。同时...

Object.prototype.doAjaxCall = function(url) {
    var bind = function(context, method) {
        return function() {
            return method.apply(context, arguments);
        };
    };

    $.get(url, null, bind(this, this.handleAjaxResponse));
};
Run Code Online (Sandbox Code Playgroud)