Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between (function(){})(); and function(){}(); [duplicate]

Possible Duplicate:
Are “(function ( ) { } ) ( )” and “(function ( ) { } ( ) )” functionally equal in JavaScript?

This is something I haven't quite figured out yet, but I have been using function(){}() just because my VIM syntax highlight screws up if I add the parenthesis, although I've seen (function(){})() around many times, maybe its an IE thing?

edit:

var singleton = function() {     // code }();  var singleton = (function() {     // code })(); 
like image 648
Luca Matteis Avatar asked Jan 08 '09 04:01

Luca Matteis


People also ask

What is the duplicate formula in Excel?

The duplicate-checking formula uses =COUNTIF to “count” which cells contain data that appears more than once throughout the spreadsheet. Resulting values can either be “TRUE” (indicating duplicate data) or “FALSE” (showing non-duplicate data).

What is the difference between function declaration and function expression in Javascript?

The main difference between a function expression and a function declaration is the function name, which can be omitted in function expressions to create anonymous functions. A function expression can be used as an IIFE (Immediately Invoked Function Expression) which runs as soon as it is defined.

What is function foo in Javascript?

Foo (pronounced FOO) is a term used by programmers as a placeholder for a value that can change, depending on conditions or on information passed to the program. Foo and other words like it are formally known as metasyntactic variables.


1 Answers

Peter Michaux discusses the difference in An Important Pair of Parens.

Basically the parentheses are a convention to denote that an immediately invoked function expression is following, not a plain function. Especially if the function body is lengthy, this reduces surprises,

like image 93
2 revs, 2 users 50% Avatar answered Oct 14 '22 20:10

2 revs, 2 users 50%