I'm working on some code where a given page has many .js files associated with it (utilizing them like libraries). Each .js file looks like this inside:
(function() {
.... all the lib functions and objects ....
})();
After some playing, I see that functions of the format (function() {...})(); get called automatically. If I strip away the outer paren's to have function() {...} then the code is invalid. If I add a function name then the code is valid but does not run till called function foo() { ... }.
Is there a special reason the lib has been written in this way? I would guess it would encapsulate variable names and such. What is it about the syntax of this that allows it to be run automatically on page load?