Init function in javascript and how it works

前端 未结 7 1856

I often see the following code:

(function () {
  // init part
})();

but I never could get my head around how it works. I find the last brac

7条回答
  •  再見小時候
    2020-12-02 06:31

    In simple word you can understand that whenever page load, by this second pair of brackets () function will have called default.We need not call the function.It is known as anonymous function.

    i.e.

    (function(a,b){
    //Do your code here
    })(1,2);
    

    It same like as

    var test = function(x,y) {
      // Do your code here
    }
    test(1,2);
    

提交回复
热议问题