JavaScript scope and closure

前端 未结 7 1954
执笔经年
执笔经年 2020-11-28 09:21

I\'m trying to wrap my head around closures (there\'s a joke in there somewhere) and I ran across this:

(function () { /* do cool stuff */ })();
7条回答
  •  我在风中等你
    2020-11-28 09:59

    That construct means declare an anonymous function and run it immediately. The reason you put your code inside a function body is because the variables you define inside it remain local to the function and not as global variables. However, they will still be visible to the closures defined inside this function.

提交回复
热议问题