By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,984 Members | 1,086 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,984 IT Pros & Developers. It's quick & easy.

What does this statement mean?

P: n/a
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

Sep 22 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a


ri*****@gmail.com wrote:

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.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Sep 22 '06 #2

P: n/a
JRS: In article <45***********************@newsspool4.arcor-online.net>,
dated Fri, 22 Sep 2006 19:31:28 remote, seen in
news:comp.lang.javascript, Martin Honnen <ma*******@yahoo.deposted :
>

ri*****@gmail.com wrote:

> 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.
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?

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Sep 22 '06 #3

P: n/a


Dr John Stockton 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?
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.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Sep 23 '06 #4

P: n/a
Dr John Stockton wrote:
JRS: ... Martin Honnen <ma*******@yahoo.deposted :
>ri*****@gmail.com wrote:
[snip]
>>( function() {
...

}) ();
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
Sep 23 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.