Possible Duplicate:
What do parentheses surrounding a JavaScript object/function/class declaration mean?
What does this javascript syntax mean?
What does this “(function(){});”, a function inside brackets, mean in javascript?
In the below code the anonymous function is being executed.
var a= 1;
var b =2;
(function() {
var b = 3;
a += b;
})();
document.write(a + " "+ b);
1) What does putting parenthesis around the function definition do?
2) What does putting () after the closing parenthesis do?
Putting the
()around thefunction() { ... }makes it an expression vs. a statement. Because it’s an expression which produces a function value the()at the end invoke the produced function.Consider the alternatives
This produces a syntax error as the
()are essentially trying to invoke a statement.This produces a
functionobject which is never invoked.