3

Possible Duplicate:
What do parentheses surrounding a JavaScript object/function/class declaration mean?

What's the meaning of round brackets in JS when used as follow:

<script>
(
 function foo(){ alert("Hello world!")}()
)
</script>

It works as expected and shows "Hello world!". But if I pull off the brackets

 <script>

 function foo(){ alert("Hello world!")}()

</script>

Chrome console generates the error "Uncaught SyntaxError: Unexpected token ) ". I can't figure out why does this happen...any help?

Community
  • 1
  • 1
  • Better duplicate: [Explain JavaScript's encapsulated anonymous function syntax](http://stackoverflow.com/questions/1634268/explain-javascripts-encapsulated-anonymous-function-syntax). – Felix Kling Jun 03 '12 at 13:15

2 Answers2

4
(
 function foo(){ alert("Hello world!");}
)

In the line above, we define a function foo which is interpreted as a function-object.

(
 function foo(){ alert("Hello world!");}
)();

We are now calling the function (using the function-object).

NOTE: The way you have used the paranthesis is actually wrong. The above is the correct way of using them.

UPDATE:

This is actually a good question. I fiddle around it for sometime and came with the following conclusion. (Note, I might be a LOT wrong, please tell me if so.)

Its about the way JS-engine parses the code. When it parses function foo(){ alert("Hello world!");}, it looks at it as a function declaration. It is (probably) not a statement that calls the function (may be according to the grammar of the language).

However, the following codes work

<script type=text/javascript>
[function foo(){ alert("Hello world!");}()]
</script>

and this one too:

<script type=text/javascript>
1,function foo(){ alert("Hello world!");}()
</script>

(and obviously the one in the question above)

I would assume this is because the JS parser expects an expression inside (..) ie when it first sees a (. This also happens with the comma-operator as in the second code above, and when initializing the array (in the first code). But when there are no tokens that make the code (the function) look like an expression, the parses throws an error when it encounters the last ().

UPDATE 2 (Many thanks to Felix Kling for this):

Please refer to the detailed explanation concerning section of the grammar in CMS's response. That answer very clearly explains what I was speaking of above ie how the JS Parses differentiates between a function-declaration and a function-expression

(
 function foo(){ alert("Hello world!")}()
)

is a function-expression. Thus it executes without any error.

But in

 function foo(){ alert("Hello world!")}<<STOP>>()

the content till <<STOP>> is parsed as a function-declaration and not as a function-expression. Hence when it encounters the first ( after <<STOP>> it flags an error.

Community
  • 1
  • 1
UltraInstinct
  • 38,991
  • 11
  • 73
  • 100
  • The question is asking why removing the outside parenthesis causes an error. You haven't explained that. – Quentin Jun 03 '12 at 11:20
  • There is nothing wrong about the way the parenthesis are used in the original question (aside, obviously, from the second example which throws the error that is the subject of the question). – Quentin Jun 03 '12 at 11:21
  • @Quentin Yes, you are correct. Updated it. – UltraInstinct Jun 03 '12 at 11:23
  • Your conclusion is right. Also see this answer: http://stackoverflow.com/a/1634321/218196 – Felix Kling Jun 03 '12 at 11:52
  • @FelixKling You are spot on! That is the precise answer to this. I was searching for specific line of grammar here: http://www.antlr.org/grammar/1153976512034/ecmascriptA3.g till you commented :) I shall update my answer with it. Thanks :) – UltraInstinct Jun 03 '12 at 11:57
  • Thank you guys! You've been really clear! – Luca Adalberto Vandro Jun 03 '12 at 12:48
0

It is a self executing function, there's another question on the subject here: What is the purpose of a self executing function in javascript?

Community
  • 1
  • 1
Filype
  • 6,921
  • 7
  • 36
  • 60
  • Wrong set of brackets. The question is about the ones surrounding the statement, not the pair together after the `}`. – Quentin Jun 03 '12 at 11:16