开发者

Is there a way for a JQuery ajax success function to access the object it's contained in?

开发者 https://www.devze.com 2023-02-18 14:14 出处:网络
I have javascript like this: function Cat() { this.meow = function() { // meow }; $.ajax( do AJAX call, success: this.meow(); );

I have javascript like this:

function Cat() { 

  this.meow = function() { // meow };

  $.ajax( do AJAX call, success: this.meow(); );

}

var TopCat = n开发者_开发问答ew Cat();

This does not work because 'this' makes no sense in the context of the success function. Is there an elegant solution?


You're looking for the context parameter to the ajax method.
It allows you to set the context in which all callbacks will be called.

function Cat() { 
    this.meow = function() { // meow };
    $.ajax({
        context: this, 
        success: function() { this.meow(); } 
    });    
}
0

精彩评论

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

关注公众号