Analyze the function by adding operators to implement immediate function execution

Source: Internet
Author: User
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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.