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

前端 未结 19 1799
走了就别回头了
走了就别回头了 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:16

    It's called a self-invoked function.

    What you are doing when you call (function(){}) is returning a function object. When you append () to it, it is invoked and anything in the body is executed. The ; denotes the end of the statement, that's why the 2nd invocation fails.

提交回复
热议问题