Briefly explain the existence of the function declaration in JS "use first, then define"

Source: Internet
Author: User

First look at a section of JS code, which uses two ways to declare two functions, respectively, in different places call two functions:

1<script>2' Use strict ';3     //Output Hello function4 console.log (hello);5     //define the Hello function6     functionHello () {7Alert ("Hello");8     }9     //Output Hi variableTen console.log (HI); One     //assign a function to Hi A     varHi =function(){ -Alert ("Hi"); -     } the     //Output Variable Hi - console.log (HI); -</script>
In JS, there are two ways to define a function, namely "function statement" and "expression".    -For the Hello function, I called it before I defined it, and I defined how the "function statement" used by the Hello function was defined. -For the HI function, I used the definition of "expression", which was also called before and after the definition. The results are as follows: we can see that functions defined in the way of "function statements" can be "used first, then defined". A function defined with an expression can only be "defined, then used". Why this happens, I talk about my personal understanding, if there is wrong place, hope can help me correct: we all know JS default has a Global object window, when running a section of JS code: The first step: The JS engine will first scan the entire JS code, all global variables are bound to the Window object, including Hello and hi these two variables. At this point, the Hello function (including the function body) and the HI variable (but the value is undefined) are available on the window. The second step: Start the execution of the JS code from the first line, although the definition of the Hello function has not been executed, but there is already a function definition in window. The hi variable is used for undefined before the assignment statement to the HI variable is executed. In summary: The order in which the two functions are defined leads to a different sequence of "definition" and "use". To verify the conjecture, we add two lines of console at the beginning of the JS code block:
1 Console.log (Window.hello); 2 Console.log (Window.hi);

The output results are as follows:

This also involves the concept of "variable promotion", which can be used to bind to the Window object to understand: before executing the JS code, all global variables (including functions and variables) are bound to the Window object, but the function will contain the body of the function, A variable is just a undefined.

I am just a novice, if there is an understanding of the deviation also hope to point out, extremely grateful!

Briefly explain the existence of the function declaration in JS "use first, then define"

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.