I was reading the jQuery source and I was wondering why the entire source file was wrapped in an anonomous function.
(function(){ ... })();
Is this something which helps not to pollute the global namespace? Why is it there and how does it work?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It uses the function body to provide its own scope rather than introducing a large number of globals that could be accidentally changed by external code.
Eg.
If the function scope were not introduced it would be possible to accidentally change
someConstantValueby introducing other code (or another library)