How can I keep the context of 'this' in jquery

前端 未结 6 1929
情话喂你
情话喂你 2020-12-31 20:11

I have something like this:

var Something = function(){
  this.render = function(){};
  $(window).resize(function(){
    this.render();
  });
}
6条回答
  •  渐次进展
    2020-12-31 20:39

    The solution you found is the the one most people use. The common convention is to call your tempThis variable "that."

    var Something = function(){
      this.render = function(){};
      var that = this;
      $(window).resize(function(){
        that.render();
      });
    };
    

提交回复
热议问题