I am very familiar with self executing functions from working with jQuery.
(function($) { /* do stuff */ })(jQuery);
Today I was reading the backbone.js source and noticed that they do this:
(function() { /* do stuff */ }).call(this);
Is this achieving the same thing? Would the following 2 lines of code do the same thing?
(function($) { /* do stuff */ })(jQuery);
(function($) { /* do stuff */ }).call(jQuery);
The first form is passing in a parameter, while the second form is setting what ‘this’ refers to inside the executing function. They are different.
For more information, see
Function.prototype.callandFunction.prototype.apply.Here’s a case where you might want to use the
calltechnique:Without setting the value of
thisviacall(), the self-calling function is invoked in the scope of the Global (window), not the current object.