The difference between one and two methods depends on the context in which they are used.
As in method 1, executing a function with call will use the first argument as 'this' within the function. In the method, two 'this' inside the function will refer to the global object because 'this' was not set by the call (except for ES 5 strict mode, where it will be undefined).
With this knowledge in your head, try to think about what will happen if both of these functions are placed in the root of the JS file that will be loaded in the browser.
In this context, 'this' passed in to 'call' in the one method will refer to the window object, which is the global object in the browser. The second method will do the same thing because functions in which 'this' are not specified by the call have their own set for the global object (except for ES5 strict mode, as indicated above).
Therefore, for this situation, they will behave identically.
However, there are situations when this is not so.
var obj = { f: function() { (function() { // 'this' here is 'obj' if called as obj.f() }).call(this); (function() { // 'this' not set by the call, defaults to 'window' (ES5 caveat) })(); } }; obj.f();
Method one is cleaner because it explicitly conveys the value of the external 'this', reducing the likelihood of confusion and adding flexibility.
The second method is shorter and simpler if 'this' does not matter.
The jQuery version is used as an easy way to always be able to access jQuery through the $$ variable, but letting the code outside jQuery have something in between.
source share