2

Possible Duplicate:
Location of parenthesis for auto-executing anonymous JavaScript functions?

I'm curious now that I have seen these two similar examples:

(function ($) {
  // code
}(jQuery));

and

(function ($) {
  // code
})(jQuery);

Is there any difference and if so what?

Community
  • 1
  • 1
Matt Kocaj
  • 11,278
  • 6
  • 51
  • 79

2 Answers2

4

There's no difference, they do the same thing. You need the parens around the anonymous function, but whether you put the parens triggering the call within those or outside them doesn't matter. Some feel the former is more "correct" (though I've never heard a strong rationale, just Crockford's assertion that it's more clear, which I contest). The latter (in my experience) is much more common.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • I get the impression that the "outer closure" (if you will) has a different effect in the 1st example - it "closes" the invocation parameter too. Not so? – Matt Kocaj Jul 28 '11 at 08:42
  • 2
    @cottsak: Not so. :-) Parentheses have no effect on the data being closed over. The only scopes in JavaScript are global scope and scopes within the bodies of functions. Curly braces (blocks) on their own, and parentheses, don't have any effect on scope. – T.J. Crowder Jul 28 '11 at 08:45
  • In your example, with the semicolon at the end, you are comparing two statements, so the semantics are _exactly_ the same. – Ray Toal Jul 28 '11 at 08:47
  • @Ray: Well, they're both expressions, we're just not assigning the result to anything. Even if we were, the result we'd be assigning would be the same (whatever the function returns -- nothing, in this case, but if it returned something, both expressions would have that as their result). – T.J. Crowder Jul 28 '11 at 08:51
  • "[...] and again, I'm wrapping the whole function and the invocation in parens. as a sign to the reader that there is something bigger going on than just an assignment of a function. There are some people who will put the golden paren. around the function and not around the whole invocation -- that doesn't make sense to me because what we are trying to tell the user is: 'Look at the whole thing', and putting parentheses around just part of it, I think, is counter-productive, so I think the whole thing needs to be wrapped in parens." -- Douglas Crockford. – XP1 Jul 29 '11 at 05:29
  • ... "Crockford on JavaScript -- Act III: Function the Ultimate" (2010-02-17). At 56:09 minutes. – XP1 Jul 29 '11 at 05:29
-1

having a pair of bracket out side the function definition makes no sense, for now, there's no diff.

these 2 equals function(){}(jQuery);

gekowa
  • 452
  • 2
  • 10
  • 1
    No, they don't equal that. That's a syntax error (**"Unexpected token ("**). There's a *reason* thousands and thousands of programmers put those parens there. – T.J. Crowder Jul 28 '11 at 08:47
  • @gekowa FYI The syntax of JavaScript prohibits any expression statement from beginning with the word `function`. Look at the [official spec](http://www.ecma-international.org/publications/standards/Ecma-262.htm) on page 223. That piece you wrote can only appear as part of a larger expression, and then in that case you have to be careful about precedence. – Ray Toal Jul 29 '11 at 00:52