On Feb 15, 7:12 pm, Peter van der Zee <[email protected]> wrote:
> On Tue, Feb 15, 2011 at 9:53 AM, Ivan S <[email protected]> wrote:
> > Hi all.
>
> > I don't know ECMA script specification so well, so if someone can explain
> > what's the difference between this two:
>
> > (function() {
>
> > }());
>
> > (function() {
>
> > })();
>
> > (brackets have different positioning)
>
> > Intuitively, I can understand the difference, but I would like technical
> > explanation.
>
> The parenthesis are actually a "grouping operator" in js. In practice this
> means that you can change the order of precedence for operators using
> parens, `5+(7*9)`

In this case, the brackets do nothing, multiplication will occur
before addition anyway. A better example might be:

 var x =  5 + 7  * 9;  //  5 + 63 =>  68
 var y = (5 + 7) * 9;  // 12 *  9 => 108


> vs `(5+7)*8`. We do it all the time, most of the time
> without even thinking about it.

But in the OP, the grouping operator does nothing functional, it can
be completely removed. It is not changing the order or precedence of
anything, the equivalent in the arithmetic example is:

 var x = 5 +  7  * 9;  //  5 + 63 =>  68
 var x = 5 + (7 * 9);  //  5 + 63 =>  68
 var x = (5 + 7 * 9);  //  5 + 63 =>  68

The main reason for using the grouping operator with functions that
are executed immediately is that it lets humans reading the code know
that when reading the first line of the function, as explained by
Peter Michaux:

"An Important Pair of Parens"
<URL: http://peter.michaux.ca/articles/an-important-pair-of-parens >

--
Rob

-- 
To view archived discussions from the original JSMentors Mailman list: 
http://www.mail-archive.com/[email protected]/

To search via a non-Google archive, visit here: 
http://www.mail-archive.com/[email protected]/

To unsubscribe from this group, send email to
[email protected]

Reply via email to