[JavaScript] +function () {} function
Gab
In JS, often encounter the following code, exactly in the function before adding a unary operator, what role?
The function is to turn the functions into self-executing functions
+function () {} () ;
The plus sign here can be substituted for ~ , * , + , - , !
other unary operators,
The effect is equivalent to:
(function () { Span class= "hljs-built_in" style= "color: #268bd2;" >console . Log ( "foo!" ); })(); //or (function () {console . Log ( "foo!" ); }());
Without this plus, the parser will assume that function is the beginning of a declaration of functions, and the latter () will result in a syntax error.
With the + sign in front of the function, it becomes a functional expression, and a function expression adds another () to become a function that executes immediately.
[JavaScript] +function () {} function