JavaScript scope and closure

前端 未结 7 1890
执笔经年
执笔经年 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 10:18

    That creates a function, calls it, and discards it.

    It might be clearer if you look at it like this:

    var throwaway = function(){
        // do cool stuff
    };
    throwaway();
    

    This is done to create a private namespace. Code in the function can have functions and variables without worrying about conflicting with other code loaded in the page.

    0 讨论(0)
提交回复
热议问题