In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the relevant knowledge of "how javascript is parsed". The editor shows you the operation process through the actual case. The operation method is simple, fast and practical. I hope this "how javascript is parsed" article can help you solve the problem.
Javascript is an interpreted scripting language that can be dynamically parsed and executed by browsers. Javascript itself is directly recognized by the browser, and the interpreter of javascript is called the javascript engine, which is the default part of the browser.
The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.
Javascript is an interpreted scripting language, which is different from the compiled language java or c #. It does not need to be compiled into a language that can be recognized by the browser, but is dynamically parsed and executed by the browser. (it is directly recognized by the browser, and the interpreter of javascript is called the javascript engine, which is the default part of the browser)
1. Code block
A code block in javascript refers to a code snippet divided by a tag.
Console.log ("this is block one"); console.log ("this is block two");
JS is compiled and executed according to code blocks, which are independent of each other, but variables and methods are shared.
Console.log (str); / / because str is not defined, the browser will make an error. The following cannot run console.log ("I am a code block one"); / / it does not run here var test = "I am a code block variable"; console.log ("I am code block two"); / / there is a run to console.log (test) / / output undefined, because the browser reports an error when the first code block executes the first line of code, and all the code below the first line in the code block is not executed. So test is defined, but there is no assignment. (why is test defined but not assigned because of the promotion of variable declarations)
In the above code, an error is reported in code block 1, but it does not affect the execution of code block 2, which is the independence between code blocks, while the variables in code block 2 that can be called to code 1 are shared between blocks.
two。 Declarative function and assignment function
Function Fn () {/ / declarative function console.log ('I am a declarative function');} var Fn = function {/ / assigned function console.log ('I am an assigned function');}
The difference between declarative functions and assigned functions is that during the precompilation period of JS, declarative functions will be extracted before executing js code sequentially.
Pre-compilation period and execution period:
In fact, the parsing process of JS is divided into two stages: pre-compilation period (preprocessing) and execution period.
During the precompilation period, JS handles all declared variables and functions in this code block, but it is important to note that only declarative functions are handled at this time, and variables are only declared but not initialized and assigned.
Fn (); / / execution result: "function 2 executed". The latter overrides the former function Fn () {/ / function 1 console.log ("function 1 executed");} function Fn () {/ / function 2 console.log ("function 2 executed");} Fn () / / execution result: "declarative function executed", the function is declared and processed during the precompilation period, so even if the Fn () call function is placed before the function is declared, it can be executed. Function Fn () {/ / declarative function console.log ("declarative function executed");} var Fn = function () {/ / assigned function console.log ("assigned function executed");} / / code block-console.log (str) / / the browser reported an error, but there was no output information / / code block 2 console.log (str); / / the console output "undefined" var str = "aaa"; / / js declared the variables during the pre-processing period, but did not initialize and assign them, so the variables in code block 2 were unfiened, while the variables in code one were nonexistent, so the browser reported an error.
Let's take a look at the following example
Fn (); / / browser reported an error: "undefined" function Fn () {/ / function 1 console.log ("function 1");}
Because the javascript engine is preprocessed and executed according to the code block, that is to say, the preprocessing is only the declaration functions and variables of the code block executed, but it is impossible to preprocess the code block that has not been loaded, which is the core of processing while compiling.
This is the end of the content about "how javascript is parsed". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.