JavaScript plus sign in front of function expression

前端 未结 3 1528
清歌不尽
清歌不尽 2020-11-22 03:28

I’ve been looking for information about immediately invoked functions, and somewhere I stumbled on this notation:

+function(){console.log(\"Something.\")}()
         


        
3条回答
  •  醉话见心
    2020-11-22 03:32

    So the short answer is that it prevents a syntax error, by using the function results in one way or another.

    You can also instruct the engine that you're not even interested in the return value by using the void operator:

    void function() { console.log("Foo!"); }();
    

    Of course, putting braces around the whole thing also serves that purpose.

提交回复
热议问题