开发者

jQuery - get reference to this

开发者 https://www.devze.com 2023-01-23 23:27 出处:网络
function Request(params) { // Stuff stuff stuff // And then $.ajax( { type: \'GET\', url: \'so开发者_开发百科meurl\',
function Request(params)
{
    // Stuff stuff stuff

    // And then

    $.ajax(
    {
        type: 'GET',
        url: 'so开发者_开发百科meurl',
        success: this.done
    });
}

Request.prototype.done = function()
{
    // "this" in this context will not refer to the Request instance.
    // How to reach it?
}


You could capture "this" first:

function Request(params)
{
    // Stuff stuff stuff

    // And then

    var $this = this;

    $.ajax(
    {
        type: 'GET',
        url: 'someurl',
        success: function() { $this.done(); }
    });
}


Apparently you can add the "context" parameter to the ajax request, like so:

$.ajax(
{
    type: 'GET',
    url: 'someurl',
    success: this.done,
    context: this
});


this is not reffering to the same thing!!!


try following:

function Request(params)
{
   var that = this;

....

Request.prototype.done = function()
{
   that...
0

精彩评论

暂无评论...
验证码 换一张
取 消