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

前端 未结 19 1538
走了就别回头了
走了就别回头了 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条回答
  •  旧时难觅i
    2020-11-22 00:56

    When you did:

    (function (msg){alert(msg)});
    ('SO');
    

    You ended the function before ('SO') because of the semicolon. If you just write:

    (function (msg){alert(msg)})
    ('SO');
    

    It will work.

    Working example: http://jsfiddle.net/oliverni/dbVjg/

提交回复
热议问题