What is the difference between these 2 JavaScript declarations?

后端 未结 5 1379
执念已碎
执念已碎 2021-01-02 05:10

For one of them the \"()\" is inside, for the other one it is outside. Here they are:

var a = (function() {
    return {
        bla: function(         


        
5条回答
  •  攒了一身酷
    2021-01-02 05:27

    There is no real difference. Both work in the same way. If you want to pass JSLint, you will need to use the first pattern, where the invocation parentheses are inside the other set of parentheses. If you don't, you will get the following error:

    Move the invocation into the parens that contain the function.

    Also note that the first set of parentheses are not required. It will work without them, but again will fail JSLint:

    Wrap an immediate function invocation in parentheses to assist the reader in understanding that the expression is the result of a function, and not the function itself.


    A couple of related questions:

    JSLint error: "Move the invocation into the parens that contain the function"

    Solution for JSLint errors

提交回复
热议问题