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

前端 未结 19 2787
清酒与你
清酒与你 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:00

    Scope isolation, maybe. So that the variables inside the function declaration don't pollute the outer namespace.

    Of course, on half the JS implementations out there, they will anyway.

提交回复
热议问题