In Google's ad js, I found a statement similar to this
//I used alert("test") to replace the statement inside
(function(){alert("test")}) ()
Tested it, similar to directly executing
alert("test")
So what are the benefits of this usage? I hope everyone can talk about it!
function xx(){...}
Equivalent to
var xx;
xx=function(){...}
function( ){....}Return a Function object
(Function object)() is to call the function
The intuitive benefit is to organize several statements that need to be executed sequentially into a unit, making the logic Clear, and there is no need to give it a name
It may be due to economical considerations: the anonymous function is like a temporary variable and can be destroyed immediately after execution, of course together with the local variables in it