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

前端 未结 1 404
天涯浪人
天涯浪人 2021-01-19 17:04

I have javascript like this:

function Cat() { 

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

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

}

var TopCat = new          


        
相关标签:
1条回答
  • 2021-01-19 17:22

    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 讨论(0)
提交回复
热议问题