Recently I learned about the differences between these three things:
var a = function() { ... };
function a() { ... };
var a = (function() { ... })();
However, I'm trying to figure out what makes the function expression work the way it does when its parsed. That is, as far as I understand, if I included (function() {...})();
on a line in my Javascript code, it would immediately execute. Is this true?
If so, what happens when the parser sees that? Is it because the function is wrapped in the parentheses or because of the appended parenthesis, or something else? I read on the MDN docs on Table 3.7 that ()
and new
are one way to call/create a new instance of something, so could I achieve the same result of (function() {...})();
with new Function(...);
? (Disregarding the fact that new Function();
takes strings).