开发者

What's the difference between these two seemingly similar closure types? [duplicate]

This question already has answers here: 开发者_运维技巧 Closed 11 years ago.

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?


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.


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

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜