Possible Duplicate:
JavaScript: Why the anonymous function wrapper?
I would like to ask you what is the reason of wrapping everything in
(function() {
document.write("Hello World!");
})();
function?
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.
Self executing anonymous function’s main purpose is to wrap everything in a private namespace, meaning any variables declared do not pollute the global namespace, basically like a sandbox.
testwould pollute the global namespace, window.test would be set.window.test is undefined, because it’s in our private sandbox.