JavaScript function declaration and Function Expression Difference function declaration promotion (Function declaration hoisting)

Source: Internet
Author: User

When the parser loads data into the execution environment, it does not discriminate between function declarations and function expressions. The parser is the first to read the function declaration and make it available (accessible) before executing any code. As for a function expression, you must wait until the parser executes to the line of code where it is located before it is actually interpreted. Example:

Alert (sum (10,10)); function sum (NUM1, num2) {    return num1 + num2;}

The above code is fully functional. Because the parser has read and added the function declaration to the execution environment before the code begins execution, through a procedure called function declaration hoisting . When code is evaluated, the JavaScript engine declares the function the first time and places them at the top of the source tree. So, even if the code that declares the function is behind the code that calls it, the JavaScript engine can lift the function declaration to the top.

If, as shown in the following example, the function declaration above is changed to an equivalent function expression, it will cause an error during execution.

Alert (sum (10,10)); var function (NUM1, num2) {    return num1 + num2;}

The code above generates an error during run time because the function is in an initialization statement, not a function declaration. In other words, a reference to a function is not saved in the variable sum until the statement where the function is executed, and because the first line of code causes "unexpected identifier" (unexpected identifier) error, it does not actually go to the next line.
In addition to the difference between when and how a variable accesses a function, the syntax of a function declaration and a function expression is actually equivalent.

JavaScript function declaration and Function Expression Difference function declaration promotion (Function declaration hoisting)

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.