Why do you need to invoke an anonymous function on the same line?

前端 未结 19 1484
走了就别回头了
走了就别回头了 2020-11-22 00:17

I was reading some posts about closures and saw this everywhere, but there is no clear explanation how it works - everytime I was just told to use it...:

//          


        
19条回答
  •  北海茫月
    2020-11-22 01:03

    There is one more property JavaScript function has. If you want to call same anonymous function recursively.

    (function forInternalOnly(){
    
      //you can use forInternalOnly to call this anonymous function
      /// forInternalOnly can be used inside function only, like
      var result = forInternalOnly();
    })();
    
    //this will not work
    forInternalOnly();// no such a method exist
    

提交回复
热议问题