I prefer to use anonymous functions because you get local variables and you don't need to create variables using var , which I find awkward in the middle of a code block.
var someClass = function() { var someElement = $('form'); this.close = function() { someElement.remove(); }; this.query = function() { (function(self, someurl){ $.ajax({ url: someurl, success: function() { self.close(); } }); }(this, someurl)); }; };
In this example, there is no need to include someurl as a parameter, however, it is useful when you want to make local copies of global variables that can change a value while waiting for a response.
source share