Immediate execution of functions in JS

Source: Internet
Author: User
This article mainly introduces the knowledge of immediate function execution in js, which has good reference value. Let's take a look at it with the small series below. 1. anonymous functions cannot be defined separately. They must be assigned values or executed immediately. Otherwise, they will be defined as syntax errors by the JS engine.

Function () {alert (dada);} VM229: 1 Uncaught SyntaxError: Unexpected token

2. You can call the function immediately after adding brackets to the function body. The function form must be a function expression, not a function declaration.

Function () {alert (123) ;}(); VM265: 1 Uncaught SyntaxError: Unexpected token

3. You can add a symbol before the function, or wrap the function in brackets to eliminate the function declaration.

(Function () {alert (123) ;}) (); undefined

4. The safest way to eliminate the function declaration is to add parentheses, because the operator number is also calculated with the return value of the function, causing unnecessary trouble.

5. The brackets of the function expression can be enclosed or not. The effect is the same.

(Function () {alert (123) ;}(); undefined

6. Immediate function execution: Creates a scope space to prevent variable conflicts or overwrites.

For more articles about immediate function execution in JS, please follow the PHP Chinese website!

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.