JS running trilogyjs running code is divided into three steps
When JavaScript code is running, it will first perform syntax analysis to check whether there are low-level errors in the code, then pre-compile, organize the internal logic, and finally start executing the code line by line. Syntax Analysis Before executing the code, the system will scan the entire code to check whether there are any low-level syntax errors, such as missing a curly brace. Precompilation Pre-compilation preludePrecompilation occurs just before a function is executed. The variable is assigned a value without being declared, and the variable is owned by the global object All declared global variables are window properties Four steps of precompilation
Let me explain this with an example. You can also give an answer yourself before continuing. function fn(a) { console.log(a); var a = 123; console.log(a); function a() {} console.log(a); var b = function() {}; console.log(b); function d() {} console.log(d) } fn(1); The first step is to create an AO (Activation Object) object. The second step is to find the formal parameters and variable declarations, and use the variable and formal parameter names as AO attribute names with values of undefined. { a: undefined, b: undefined, } The third step is to unify the actual parameters and formal parameters { a: 1, b: undefined, } Step 4: Find the function declaration and assign the value to the function body { a: function a() {}, b: undefined, d: function d() {} } So just before the function fn is executed, the values of a, b, and d are as shown above So the result of executing fn(1) is
In the global scope, the precompilation process is slightly different
Interpretation Execute code line by line Practice Questions Here are a few examples, if you are interested, you can take a look function test(a, b) { console.log(a); console.log(b); var b = 234; console.log(b); a = 123; console.log(a); function a() {} var a; b = 234; var b = function() {}; console.log(a); console.log(b); } test(1); global = 100; function fn() { console.log(global); global = 200; console.log(global); var global = 300; } fn(); var global; function test() { console.log(b); if (a) { var b = 100; } c = 234; console.log(c); } var a; test(); a = 10; console.log(c); SummarizeIn most cases, we use the following method to handle the precompilation process:
If you encounter a complex situation, you can only use the most primitive way to solve the problem. This is the end of this article about understanding JavaScript precompilation (summary). For more relevant JavaScript precompilation content, please search 123WORDPRESS.COM’s previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: How to export CSV file with header in mysql
>>: Solution to the problem that crontab output redirection does not take effect in Linux
This article shares the specific code for React t...
Two methods to implement Mysql remote connection ...
There are many database management tools for MySQ...
Table of contents 0. What is Module 1.Module load...
Preface The electricity in my residence has been ...
This article describes how to install the PHP cur...
A design soldier asked: "Can I just do pure ...
Docker allows network services to be provided by ...
Table of contents 1. What is block scope? 2. Why ...
This article example shares the specific code of ...
Prepare a CentOS6 installation disk (any version)...
Table of contents Preface Can typeof correctly de...
1. The difference between forward proxy and rever...
Ubuntu's own source is from China, so the dow...