Brackets and semicolon after function definition
1 answer
The code you are showing does not compile.
With the parentheses added, this would be a named immediately called function:
(function xyz(){ //function code here ;
}) ();
Most often, these functions are anonymous:
(function(){
var a; // a isn't visible outside
// code using a
})();
The code is directly called, as with no function definition, but the point of such a function is to define a scope (which can only be a global scope or a function) so that the scope variable does not flow into the attached one. This pattern is very useful for keeping your code clean: you don't add any variable to the outer scope, and you cannot remove an existing one.
Here, the function also has a name that can be used internally for recursion.
(function xyz(){
var a; // a isn't visible outside
// code using a and calling xyz
})();
+3
source to share