Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery ajax scope memory leak

I wonder whether memory leak can occur in the following pseudo jQuery code (because the success callback always have a reference to _this)?

var _this = this;

$.ajax({
  url: "foo",
  type: "POST",
  data: data,
  success: function() {
      // Do stuff with _this
      _this.doStuffs();
})
like image 803
trivektor Avatar asked Nov 12 '22 18:11

trivektor


1 Answers

Is this code within a function ?

If yes (which I guess), _this will be illegible to garbage-collection right after the ajax call has completed or failed. So there is no reason in theory to worry about memory leak here.

like image 80
Samuel Rossille Avatar answered Nov 15 '22 06:11

Samuel Rossille