JavaScript is a descriptive scripting language. It is different from compiling languages such as java or C #. It does not need to be compiled into an intermediate language, but is dynamically parsed and executed by the browser. If you cannot understand the running mechanism of the javaScript language, or simply put, you cannot master the execution sequence of javascript, then you are like bole cannot control qianma. Although modern browsers can download JavaScript in parallel (Some browsers), their execution is still carried out in the order of introduction considering the JavaScript dependency.
This article records some of the things I have learned in JavaScript, deepen my memory and record it, so that I can easily review it later.
Execution sequence in html document
By comparing the execution sequence of js code, you can intuitively feel the execution sequence. However, the execution sequence of js Code is complicated. Sometimes we write JavaScript code in html, and the html document parsing process in the browser is like this: the browser parses the Page Structure and information from top to bottom according to the document stream. Js Code, as an embedded script, is also part of the html document. Therefore, the execution sequence of js Code during loading is determined by the sequence in which the script tag script appears. (The following is a chestnut)
Script console. log ("top script"); script
DocumentScript console. log ("header script"); scriptScript console. log ("page script"); scriptScript console. log ("bottom script"); script
For external js file scripts imported using the src attribute of script, it will also be executed according to the order in which the statements appear, and the execution process is part of the document loading, the execution will not be extended because it is an external js file.
// Load B. js first and execute the code in it