What does this statement mean?

R

riceyeh

Hi,
When reading the source code of dojo, it uses the following statement
quite often. What does it mean? What I do not understand is it has a
function definition enclosed in a pair of parenthesis and the the last
pair of parentheses.

( function() {
...

}) ();


Rice
 
M

Martin Honnen

When reading the source code of dojo, it uses the following statement
quite often. What does it mean? What I do not understand is it has a
function definition enclosed in a pair of parenthesis and the the last
pair of parentheses.

( function() {
...

}) ();

It is an expression statement that defines an anonymous function with a
function expression and then calls it directly.
 
D

Dr John Stockton

JRS: In article <[email protected]>,
dated Fri, 22 Sep 2006 19:31:28 remote, seen in
news:comp.lang.javascript said:
(e-mail address removed) wrote:



It is an expression statement that defines an anonymous function with a
function expression and then calls it directly.

What benefits does it have over just writing the body, apart from
enabling variables to be declared locally with no outside effect?

Are the opening parenthesis and its mate necessary?
 
M

Martin Honnen

Dr John Stockton wrote:

What benefits does it have over just writing the body, apart from
enabling variables to be declared locally with no outside effect?

I think the benefit you describe (local variables with no side effect)
is the reason for doing it. And you do not have a function name that is
in scope which you would have if you used a function declaration first
and then called that function by its name.
Are the opening parenthesis and its mate necessary?

Yes, to ensure the function stuff is parsed as an expression, an
expression to create a function, and not as a function declaration.
 
M

Michael Winter

Dr said:
JRS: ... Martin Honnen said:
(e-mail address removed) wrote:
[snip]
It is an expression statement that defines an anonymous function with a
function expression and then calls it directly.

What benefits does it have over just writing the body, apart from
enabling variables to be declared locally with no outside effect?

That's about the only reason to use an expression statement like that:
establish a new execution context, declare variables and functions
within it, but don't expose any of them to other code.

There are variations, such as:

var identifier = function() {
/* ... */

return /* ... */; /* An object or function reference,
* usually.
*/
}();

and:

(function() {
/* ... */

this.propertyName = /* ... */;
this.anotherProperty = /* ... */;
})();

but the aim is generally the same.
Are the opening parenthesis and its mate necessary?

Yes. Without the parentheses, the function token will be considered the
start of a function declaration, rather than a function expression:
expressions cannot start with either a function token, or an opening
brace (to avoid confusion with an object literal). Function declarations
must have an identifier, and as they do not evaluate to anything (they
are statements), the function object created by the declaration cannot
be called simply by appending a pair of parentheses. The latter would be
considered grouping parentheses missing the required, contained
expression. In short:

function() {
/* ... */
}();

is a twofold syntax error.

Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,201
Messages
2,571,049
Members
47,655
Latest member
eizareri

Latest Threads

Top