Understanding the structure of underscore.js code

javascript, underscore.js

Solution

Since both are equivalent in normal context I looked into the source and it was changed from the form you suggest to the current form 2 years ago with the following check-in comment:

"Added explicit definition of global context for compatibility with Adobe JS"

https://github.com/jashkenas/underscore/commit/aa916b8cfe565dc206d85c4cb74fbb6c499067a7

The check-in logs for the first version of Underscore with this change claims "Improved Underscore compatibility with Adobe's JS engine that can be used to script Illustrator, Photoshop, and friends."

http://underscorejs.org/ Version 1.4.3

So this change seems to have been made because Adobe's JavaScript engine did not conform to ES3 or ES5 at the time but with this change Underscore was made compatible with their variant.

If you are not planning on running your module in Adobe JS then you can use either form. If you are then it appears Adobe JS requires the form used by Underscore.

Problem

I saw this pattern in the underscore source code and many other open source JavaScript projects : ``` (function() { // the library code }).call(this); ``` Anyone can explain what this pattern do ? and what is the benefits of using it ? Why not just : ``` (function() { // the library code }()); ```

Original source