The backbone.js source code uses a function wrapper like this:
(function(){
...
}).call(this);
as seen at http://backbonejs.org/docs/backbone.html#section-185.
Much more often, I’ve seen the following used instead:
(function(){
...
})();
When does the behavior of these two differ? I was under the impression that they were equivalent, but I assume that there must be a difference given that Backbone uses .call(this) instead of the shorter alternative.
In the first example,
thisinside of the function will be thethisfrom the calling scope.In the second example,
thiswill bewindow.(As noted by Šime Vidas, it’s
undefinedin strict mode, rather thanwindow.)