1

What is the exact difference between these two lines of code.

 var functionOne = (function(){})();

and

 var functionTwo = (function(){}());

I've noticed that both were being used while looking through js Module pattern, but i'd like to know what is exact difference between them.

user1033698
  • 89
  • 1
  • 7

1 Answers1

1

Both are the same Immediately-Invoked Function.
There few different syntax variations. As Douglas Crockford’s JSLint offers the right declaration for self-invoking functions is:

(function () {
    //body
}());

Alternative syntax is, which Crockford calls “dog balls”…:

(function () {
    //body
})();
CD..
  • 72,281
  • 25
  • 154
  • 163
  • 1
    But that's just his opinion. They mean exactly the same thing. – Quentin Mar 09 '14 at 18:15
  • 1
    Calling that declaration the *right* declaration is itself not right :) That's personal preference – CodingIntrigue Mar 09 '14 at 18:17
  • 1
    the first one is more readable and therefore preferable. when a coder sees the enclosing parens, they know to work back to the left to figure out what's being called. with dogballs, there's a potential that it's a returned function being invoked instead of the wrapper. everything else equal, readability is right. – dandavis Mar 09 '14 at 18:18