What does this statement mean?

  • Follow


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

0
Reply riceyeh (2) 9/22/2006 5:12:03 PM


riceyeh@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/
0
Reply Martin 9/22/2006 5:31:28 PM


JRS: In article <45141df0$0$26952$9b4e6d93@newsspool4.arcor-online.net>,
dated Fri, 22 Sep 2006 19:31:28 remote, seen in
news:comp.lang.javascript, Martin Honnen <mahotrash@yahoo.de> posted :
>
>
>riceyeh@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.htm> jscr maths, dates, sources.
 <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
0
Reply Dr 9/22/2006 8:25:13 PM


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/
0
Reply Martin 9/23/2006 12:11:29 PM

Dr John Stockton wrote:

> JRS: ...  Martin Honnen <mahotrash@yahoo.de> posted :
>
>> riceyeh@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
0
Reply Michael 9/23/2006 12:27:36 PM

4 Replies
161 Views

(page loaded in 0.11 seconds)

Similiar Articles:













7/24/2012 5:56:47 PM


Reply: