This article analyzes how to implement immediate function execution by adding operators before the function. We know that the function call method is usually FunctionName ()
However, if we try to add () to the end of a "definition function", the parser cannot understand it.
Function msg () {alert ('message') ;}(); // The parser is incomprehensible.
Define the function call method as msg ().
To immediately execute a function, you can wrap the function body.
This is because the parser uses parentheses to wrap the definition function body and calls the definition function in the form of a function expression. That is to say, any way to convert a function into a function expression can make the parser correctly call and define a function. As follows:
// An error will be reported when writing this statement, because this is a function definition: function () {} () // common (with a pair of parentheses added), calls anonymous functions :( function () {}) () // Add a boolean operator (only one exclamation point is added) to the front, which is the expression. The code that follows will be executed and the call is legal! Function (){}()
! Only one operator, while +-| ~ All have such functions.
Yes! It may be more of a habit. Different operators have different performance.
The effect is equivalent to the following two commonly used instant execution methods:
(Function () {}) (); // or (function (){}());
Thank you for reading this article.
The above is the detailed analysis of how to implement immediate function execution by adding operators before the function. For more information, see other related articles in the first PHP community!