0

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

11
  • 2
    What do you mean by "at a lower/language level?" Commented Jun 25, 2014 at 15:48
  • The third is an example of IIFE or immediately-invoked function expression. Try Googling it, or self-invoking function. Commented Jun 25, 2014 at 15:48
  • Do you want to know how the parser parses the JavaScript code? Commented Jun 25, 2014 at 15:48
  • stackoverflow.com/questions/336859/… and stackoverflow.com/questions/8228281/… Commented Jun 25, 2014 at 15:49
  • 1
    I suppose you could decompile the browser. Language parsing is a fairly well-known technique. Commented Jun 25, 2014 at 15:53

1 Answer 1

1

if I included (function() {...})(); on a line in my Javascript code, it would immediately execute. Is this true?

Yes. It's called an immediately executed function expression.

Is it because the function is wrapped in the parentheses or because of the appended parenthesis, or something else?

The wrapping parentheses are of no significance to the execution, you could in fact omit them (when you use the result in another expression, to avoid the syntactic ambiguity with a function declaration) or place them differently. However, the "appended parenthesis" are just a plain function call: anything_that_resolves_to_a_function()

I read on the MDN docs on Table 3.7 that () and new are one way to call/create a new instance of something.

Yes. You shouldn't use new function() { … } though.

so could I achieve the same result of (function() {...})(); with new Function(...);?

No. The Function constructor is a completely different beast.

Sign up to request clarification or add additional context in comments.

11 Comments

Unless I misunderstand what you're saying, the wrapping parentheses are significant because they're what make it an expression instead of a declaration. You can't just write function f() { } ().
@Alnitak: OP is assigning to var a, so it's an expression anyway. It makes a difference only if it the top expression of an expression statement.
I think you've lost some context in your answer - sure, var a = ... is definitely an expression, but you don't refer to that, and the only previous use of function in your answer is (function() { ... })() which emphatically does need the wrapping parentheses.
Although I now know what you meant, I think your answer is still unclear - it still looks like you're referring to the earlier (function() { ... })() and not the omitted part of the OP's question.
@JoshuaSmock yes - the appended parentheses invoke the function (expression), and like a normal function call can also include parameters.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.