An Analysis of javascript anonymous functions _ javascript skills

Source: Internet
Author: User
An Analysis of javascript anonymous functions. If you are studying javascript, you can refer to the anonymous function, that is, a function without a name. For example:

The Code is as follows:


Function (){
Alert ('a function ');
}


However, the above Code will report an error. Firebug prompt: function statement requires a name, that is, the function must have a name.

The strange thing is that if I use a pair of () to package the function with no name, no error will be reported. For example:

The Code is as follows:


(Function (){
Alert ('a function ');
})


(Note the () of the function package ()!). Although no error will be reported in this way, who knows if this function is declared successful? Is it because there is no declaration? Let's test: Let the function execute it once:

The Code is as follows:


(Function (){
Alert ('a function ');
}())


As you can see, the function is executed, indicating that the function exists.

Similarly, if you remove the wrapped function () at this time, the previous error will still be reported and the function cannot be executed...

The Code is as follows:


Function (){
Alert ('a function ');
}()


This is so important to the () function of the package? Which expert explains the principle?
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.