Hey Diego,

Yes, that's of course correct (but it seems was easy for to answer, that's why I wanted that OP answered ;). Anyway, yes, your answer is completely correct and as I mentioned at the beginning it's related with ASI.

(Another change which will avoid the syntax error is to return a function from the "foo" in version 1. Though, being syntactically correct such a program obviously semantically and logically is buggy).

Dmitry.

On 16.02.2011 16:47, Diego Perini wrote:
Dmitry,
if you correctly insert the missing semicolons both versions work,
without semicolons both version fail.

Version 1:

var foo = function () {
  return "foo";
};

(function () {
  return "IIFE";
})();

Version 2:

var foo = function () {
  return "foo";
};

(function () {
  return "IIFE";
}());

Try them in a console, the correct result is when the "IIFE" string is
printed out.

Without semicolon, "Version 2" will not throw errors but will not
produces the "IIFE" string either.

The problem is that in "Version2" the "IIFE" string just become a
parameter to the self invoking function resulting by omitting the
semicolons.

var foo = function () { return "foo"; }(function () { return "IIFE"; }());
var foo = function () { return "foo"; }("IIFE");

ASI is hard.

--
Diego


On Wed, Feb 16, 2011 at 12:26 PM, Dmitry A. Soshnikov
<[email protected]>  wrote:
The only observable difference is a very specific. It related with ASI
(automatic semicolon insertion) mechanism. E.g.

Version 1:

var foo = function () {
  return "foo";
}

(function () {
  return "IIFE";
})();

Version 2:

var foo = function () {
  return "foo";
}

(function () {
  return "IIFE";
}());

The version 1 fails, the second one -- does not. Why? Which changes to
provide for that version 1 don't fail?

But as mentioned, this case is very specific and just academical. In real
practice, only the convenience in this case does matter.

(function () {})(); -- here first we get a FE (evaluating the expression
inside the grouping operator) and the call it.

(function () {}()); -- here we evaluate the grouping operator which directly
evaluates the function execution.

Additional info:
http://dmitrysoshnikov.com/ecmascript/chapter-5-functions/#question-about-surrounding-parentheses

Dmitry.


On 15.02.2011 11:53, Ivan S 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.


Thank you very much.



Ivan
--
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]
--
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]


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