Home > Web Front-end > JS Tutorial > body text

In-depth understanding of the execution order of javascript_Basic knowledge

WBOY
Release: 2016-05-16 16:53:24
Original
984 people have browsed it

If you cannot understand the operating mechanism of the JavaScript language, or simply put, you cannot master the execution sequence of JavaScript, then you are like Bole who cannot control a thousand-mile horse and let the thousand-mile horse break free and run around.

So how does JavaScript parse? What is its execution order? Before understanding these, let’s first understand a few important terms:

1. Code blocks
Code blocks in JavaScript refer to code segments separated by <script> tags. For example: <br></p> <div class="codetitle"> <span><a style="CURSOR: pointer" data="50623" class="copybut" id="copybut50623" onclick="doCopy('code50623')"><u>Copy code</u></a></span> The code is as follows:</div> <div class="codebody" id="code50623"> <br><script type="text/javascript" ><br> alert("This is code block one");<br></script>

JS is compiled and executed according to code blocks. The code blocks are independent of each other, but variables and methods are shared. What does it mean? For example, you will understand:

Copy the code The code is as follows:



In the code, an error is reported when running in code block one, but it does not affect the execution of code block two. This is the independence between code blocks, and the variables in code one can be called in code block two, which is sharing between blocks.

2. Declarative functions and assignment functions


Function definitions in JS are divided into two types: declarative functions and assignment functions.

Copy code The code is as follows:



//js declares variables during the preprocessing period, but does not initialize and assign values, so the variables in code block two are unfiened, while the variables in code one are It doesn't exist at all, so the browser reports an error.


After understanding the above terms, I believe everyone has a rough impression of the operating mechanism of JS. Now let’s look at an example:

Copy Code




Why does the browser report an error when running the above code? Wouldn't the declared function be processed during the preprocessing period? Why can't the Fn() function be found? In fact, this is a misunderstanding. We said above that the JS engine is executed sequentially according to code blocks. In fact, it should be preprocessed and executed according to code blocks, which means that only the executed code is preprocessed. Blocks declare functions and variables, and code blocks that have not yet been loaded cannot be preprocessed. This is also the core of processing while compiling.

Now, let us summarize and organize it:

Copy the code

The code is as follows: step 1. Read the first code block. Step 2. Do syntax analysis. If there is an error, a syntax error will be reported (such as mismatched brackets, etc.) and jump to step5. Step 3. Perform "pre-compilation processing" on var variable and function definitions (no errors will ever be reported, because only correct declarations are parsed).
Step 4. Execute the code segment and report an error if there is an error (for example, the variable is undefined).
Step 5. If there is another code segment, read the next code segment and repeat step 2.
Step6. End.


According to the execution order of the HTML document flow, the js code that needs to be executed before the page element is rendered should be placed in the <script> code block in front of <body>, and it needs to be in the page element The loaded js is placed behind the </body> element, and the onload event of the body tag is executed at the end. <br><br><br> </div> <br>Copy code<br><div class="codetitle"> <span> The code is as follows:<a style="CURSOR: pointer" data="50219" class="copybut" id="copybut50219" onclick="doCopy('code50219')"><u></u><script type="text/javascript"> </a> alert("first");</span> function Fn(){</div> alert("third");<div class="codebody" id="code50219"> }<br></script>







Related labels:
source:php.cn
Previous article:Usage examples of typeof operator in JavaScript_Basic knowledge Next article:jQuery scroll event to implement monitoring scroll bar paging example_jquery
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Latest Issues
Related Topics
More>
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template