JavaScript: Why shell anonymous functions?

Very often, for libraries / functions / snippets, Javascript is encapsulated by an anonymous function, e.g.

(function () { // code to be executed goes here })(); 

As far as I can tell, this should be completely redundant: this anonymous function simply executes the code inside. However, obviously, the reason for this !:-)

I saw the appearance of this with a comment next to the first line: //// wrapper for Opera.

Does anyone know why this code is commonly used, what is the history of its development and what is its relationship with Opera?

0
javascript
source share

No one has answered this question yet.

See similar questions:

345
Explain the syntax of an encapsulated anonymous function
269
What do the parentheses associated with declaring an object / function / class mean?
77
How does the constructor (function () {}) () work and why do people use it?
7
(function () {}) () Declaring / initializing a javascript function
2
standalone parentheses in javascript
one
javascript syntax and jQuery source
0
What is the purpose of the anonymous JavaScript function enclosed in parentheses?

or similar:

7649
How do JavaScript locks work?
7494
How to remove a specific element from an array in JavaScript?
7432
How to check if a string contains a substring in JavaScript?
7287
What does use strict do in JavaScript, and what are the reasons for this?
6493
var functionName = function () {} vs function functionName () {}
5722
How to remove a property from a JavaScript object?
5670
Which operator is equal (== vs ===) should be used in comparing JavaScript?
5101
What is the most efficient way to deeply clone an object in JavaScript?
4829
How to include a javascript file in another javascript file?
4380
For each array in javascript?

All Articles