What is the purpose of a self executing function in javascript?

前端 未结 19 2797
清酒与你
清酒与你 2020-11-21 04:22

In javascript, when would you want to use this:

(function(){
    //Bunch of code...
})();

over this:

//Bunch of code...
         


        
19条回答
  •  独厮守ぢ
    2020-11-21 05:08

    One difference is that the variables that you declare in the function are local, so they go away when you exit the function and they don't conflict with other variables in other or same code.

提交回复
热议问题