JavaScript Run command

Source: Internet
Author: User

Preface

Moving JS for some time, I think things are just a superficial understanding. is very deficient. So start using blogs to make a slow recording and accumulation of this part of the knowledge.

Believe that add up, slowly will this part of the knowledge to conquer!

The first record is not related to the application. But very low-level knowledge---javascript parsing engine.

The knowledge that you want to know about this part is also associated with problems encountered in your project.
The background of the problem

In a script, the order of running is to first run the alert () in the JS file, and then run the function when I call the function in detail. When I saw it, there was a question: Why did you run alert ()before running the function, and it was obvious that he didn't run the function I defined but ran alert ();


The result of the operation is: Test 1


Prerequisites to understand

Two concepts need to be understood, one is the JavaScript parsing engine. The other is the relationship between the JavaScript parsing engine and the browser.

1.JavaScript parsing engine Explains the program that runs the script.

Can be seen as an interpreter.


This engine needs to be completed with two functions:
One is to explain the script program, the JS code read.
The second is to run the script program, read the script program to run the program.


For example, see a sample in a blog post. When you write var a = 1 + 1; In this piece of code, what the JavaScript engine does is read (parse) Your code and change the value of a to 2.
When you look at this concept, another concept is the compiler, which simply compiles the source code into a second type of coding (for example, machine code). or byte code). It seems to be an interpreter who translates Chinese into English. It is not possible to run this program.
2.JavaScript parsing engine and browser relationship The JavaScript parsing engine is one of the parts of the browser.
when we understand the premise, we know that the JS code we write is required to parse the JavaScript parsing engine in the browser. Run, detailed resolution mechanism everyone can Google, I do not understand this part of the deep, just know about each. But I think this knowledge is enough for me to know other knowledge.
The parsing mechanism is divided into two large processes, one compiling process and the other running process. The compilation process is finally building a syntax tree in memory. The running process is to run the code according to the syntax tree. I'm not sure about compiling and running the internal verbose run.


The order in which the JS code runs in the pagethe above is all about theoretical knowledge. Let's take a look at the sequence of JS code running in the page in the actual application process. These are some very image reactions to the above theoretical knowledge.

1. Engine-to-JS parsing---Pre-compile and run sequence relationshipsIt handles all declared variables and functions during the pre-compilation period. Therefore, there will be no error when the JavaScript interpreter runs the following script:
The operating result is: undefined test 1

Pre-compiled work: handle declared variables and functions. So that it is visible to all code during the run.

But you will also see. Run the above code. The value of the hint is undefined, not 1.

This is because the variable initialization process occurs at run time, not the precompiled period.

At run time, the JavaScript interpreter is parsed in code order, assuming that the variable is not assigned a value in the preceding line of code. The JavaScript interpreter uses the default value of undefined. Because the variable a is assigned in the second row. So in the third line of code, the value of variable A is 1, not undefined.


2. File stream loading---JS runs in HTML document flow order

JS can be seen as part of an HTML document.

HTML documents are parsed from top to bottom. This is true whether you use a <script></script> block or a JS file that uses external references.

Use the external JS file reference to write the above code in the JS file. The running result is the same

JS file

var A; a = 1;function F1 () {alert ("first function");} Alert ("Test"), function F2 () {alert ("second function");} alert (a); function F3 () {alert ("third function");}


Running Result: Test 13. After the file stream is loaded---Change the JS run sequence according to the event mechanism

This is similar to calling a function, where it is called to run here, assuming no call to run. You can see it from the code above. Added a click event to the a tag. So the corresponding function will be run when clicked.

Summarize

So it is possible to explain why alert () is running, and the function is not running. The JS parsing engine has a pre-compilation process that handles the defined variables and functions.

JS also runs in the order of the HTML document stream at the same time.

This is the process before running my custom function, and my running custom function is actually a manifestation of the event mechanism invoking JS.



Copyright notice: This article blog original article. Blogs, without consent, may not be reproduced.

JavaScript Run command

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.