JavaScript scripts parse from the loading browser to the display execution process

Source: Internet
Author: User

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Simple code:

    <ScriptType="Text/javascript"Src="Xxx.js" ></script> <script type="Text/javascript" > var i = 10; function say(msg) {alert (msg);} </script> <script type="Text/javascript" > j=; Say (" Hello World "); </script>                

The preceding code snippet runs in the following order:

Step1. Read in the first code snippet
Step2. Do grammatical analysis, error is reported grammatical errors (such as parentheses mismatch, etc.), and jump to Step5
Step3. Create (global/function) execution environment, variable object creation and instantiation, scope allocation, and "pre-parse" var variables and function definitions, which belong to the Declaration phase
Step4. Execute code Snippet (call function, enter eval, create new execution Environment), error (e.g. variable undefined)
Step5. If there is a next code snippet, read the next code snippet and repeat Step2
Step6. End

Additional explanations:

1, for Step1 ' code snippet ' refers to the contents of the <script>... ...</script> tag, but also includes the external introduction of the script file, such as is <script src="xxx.js"></script> also listed as the category of code snippets. [Each <script> tag provides a compiled and immediately executed compilation unit, because the linker is missing, and JavaScript throws them into a common global namespace.] – The language of JavaScript ("pristine")

2, for STEP3 to create a global execution environment, not every piece of code to execute STEP3, in fact, a page will only create a global execution environment, so from STEP2 repeat, not including step3.

3. For STEP3, variable declarations and function declarations are promoted to the top of the current scope, ' Pre-parsing ' process, all variables are assigned to the undefined value, the function is assigned to the function definition of the string, and the function body of the JS code does not do special processing, just the function body JS code scan results stored in the function object's internal properties, and further processing when the function is executed.

For example: The popup result is not a computed value, but a defined string of a function

    <script type="text/javascript"> function a(){ var b =1; var c= 2; var d = b+c; alert(b+c); } alert(a); </script>

4, for STEP3, create the function execution environment, that is, the variable object that creates the function (the active object), declares the variable and the function, is when the function is called, and the variables and functions defined inside the function are stored in the change amount object.

For example:

<script type="text/javascript"> function test(){ //未用var声明,a属于全局变量,如果未调用test就完成了声明,则全局环境可以访问到a变量 a = 2; } //alert(a);//Uncaught ReferenceError: a is not defined //alert(window.a);//undefined,并不能说明全局变量中已经声明了a变量,[如果你尝试检索一个并不存在的成员元素的值,将返回一个undefined值。--《javascript语言精粹》] test(); alert(a);//调用之后,则可以访问a变量 </script>

Summary process: Download, read, syntax analysis, code parsing, execution

Reference Source: http://www.cnblogs.com/fool/archive/2010/10/16/1853326.html

JavaScript scripts parse from the loading browser to the display execution process

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.