Anonymous functions in JavaScript meet! What's going to happen?

Source: Internet
Author: User

Typically, we declare a function test) {}, which can be called by Test (). However, if we add () at the end of this function declaration, the parser is incomprehensible.

function Test () {    console.log (' Hello world! ' );} (); // The parser is not understandable. 

So why wrap the function body part in ()?

(function  Test () {    console.log (' Hello world! ')  // output Hello world!

When the interpreter interprets a statement, it is understood as a function declaration if it starts with the functions. Instead of using parentheses to define the function body, the parser will handle the defined function in the form of a function expression, and adding another () after the function expression becomes a function that executes immediately. in other words, any method that eliminates ambiguity between function declarations and function expressions can be correctly identified by the parser. So, the assignment, the logic, even the comma, various operators can tell the parser, and the function to use the unary operation can be counted as the fastest way to eliminate ambiguity, the common exclamation point is just one of them. Any method that can turn a function into a function expression allows the parser to call the definition function correctly.

For example:

! function () {    console.log (' Hello world! ' );} (); // Output Hello world

and omitted! The words:

function () {    console.log (' Hello world! ' );} ();

is understood as a function declaration, and the function declaration does not have a name to give an error. If there is a function name:

function f () {Console.log (' Hello world! ' );} ();

will still be an error, because function f () {} functions declaration is promoted, equivalent to:

function f () {    console.log (' Hello world! '  // other code ...  //  error here. 
Ps:

If you don't care about the return value, these unary operations are valid

+function() {alert (' Hello world! ')} ()        //  NaN-function() {alert (' Hello world! ')} ()        //  NaN~function() {alert (' Hello world! ')} ()        //  -1

If you have any questions, please leave a message (???)

Anonymous functions in JavaScript meet! What's going to happen?

Related Article

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.