search
HomeWeb Front-endJS TutorialUnderstand the compilation principles, scope, scope chain, variable promotion, and temporary Zenless Zone Zero of JS series (1) in one paper

Understand the compilation principles, scope, scope chain, variable promotion, and temporary Zenless Zone Zero of JS series (1) in one paper

Write at the front: This is the javascript column I am about to start writing a series, Mainly in the era when frameworks are rampant, although you use frameworks for work, for interviews and technical advancement, the foundation of basic JS knowledge is the icing on the cake, and it is also a piece of knowledge that you have to learn. Although you don’t need to know a lot about cars to drive a car, Just master the common functions of the car. But if you know cars, you can drive better, and by the same token. Of course, an article will not just talk about one knowledge point. Generally, related knowledge points will be connected in series. While recording your own learning, you will share your own learning and encourage each other! If you can, please give me a like. Your like will also make me work harder to update!

##Consuming time: 5-10 minutes
  • Difficulty:
  • Easy
  • , don’t run, walk after reading

JS compilation principleLet’s look at a line of code first
var name='jack';复制代码

In our eyes, this is something that can be done with one line of code or one statement, but , in the eyes of JS, the code of this sentence should be presented in the following way

var name;    //编译阶段处理name='jack';    //执行阶段处理复制代码

If you find that this is different from the JS you originally knew, that is because JS compilation is mainly divided into two parts There are two phases, the compilation phase and the execution phase. Let us first take a look at what these two phases do respectively:

  • Compilation phase

    The protagonist of this stage is the so-called compiler , the compiler will search the current scope to see if a variable named name already exists. If it already exists, then do nothing, just ignore the statement var name and continue compiling; if not, add a new variable called name in the current scope. . Then, the compiler will generate the code required for the engine to run, and the program will enter the execution phase

  • Execution phase

    The protagonist of this stage is the familiar JS engine. When the JS engine is running, it will also search the current scope first. See if there is a variable called name. If there is, then it is just right and assign the value directly. If not, it means that the current scope does not exist. What should I do? Then consider sticking your head out. , go outside (parent scope) to see if there is one. If not, go outside and search, layer after layer (of course, if there is a parent layer). If it is still not found in the end, then the JS engine will also If it means you are powerless, then throw an exception and show it to others to show that you have tried your best.

    The above-mentioned search outside, layer after layer, from the current scope to the parent scope, to the parent's parent scope, and so on, is the so-called scope Chained, just like a chain, each link goes up one by one. The description can be said to be very appropriate. To sum up, scopes within scopes, there is the so-called scope chain

作用域

  • 定义

    大家都知道,变量最基本的能力就是能够存储变量当中的值、并且允许我们对这个变量的值进行访问和修改,而对于变量存储,访问的一套规则,就是所谓的作用域

  • 分类

    • 全局作用域

    在任何函数外或者代码块之外的顶层作用域就是全局作用域,而里面的变量就是全局变量

    var name='jack';   //全局作用域function showName(){    //函数作用域  console.log(name);}{  name='test';    //块级作用域}showName();    //test复制代码

    可以看到全局变量,无论是在全局作用域,函数作用,还是块级作用域中都可以正常访问

    • 函数作用域

    在函数内的作用域就是函数作用域

    function showName(){  var name='jack';    //函数作用域}showName();   //方法调用{  console.log(name);  //块级作用域,Uncaught ReferenceError: name is not defined}console.log(name);  //全局作用域,Uncaught ReferenceError: name is not defined复制代码

    可以看到函数内部变量,在全局作用域以及块级作用域中,都无法访问,只有在函数内部,才能访问的到,所以函数内的变量也被称为局部变量

    • 块作用域

    ES6 中新出的两个新关键字 letconst中,自带块级作用域,块级作用域相当于是只在这块代码块中生效,如果它被大括号 {} 所包围,那么大括号中就是一段代码块,代码块中使用 letconst 声明的变量也被称为局部变量

     {   let name='jack'; } console.log(name);    //Uncaught ReferenceError: name is not defined function showName{   console.log(name); } showName();    //Uncaught ReferenceError: name is not defined复制代码

    可以看到块级作用域中的变量,出了那个代码块,就找不到了

其实上面的三种情况,结合JS编译原理和作用域链向外不向内查找,思考一下,也不难理解

作用域链

回到作用域链,其实在上面已经解释的差不多了,作用域和作用域的嵌套,就产生了作用域链,另外要记住的一个特性就是作用域链的查找,向外不向内,想想探出头去,而不是看着锅里,就可以了

变量提升

先来看一段代码

name='jack';console.log(name);    //jackvar name;复制代码

你会发现,这段代码不会发生报错,并且能正常地运行,结合上面所说的JS编译原理,你就能想到,在JS的眼中,它的代码实际上是这样子的,这就是所谓的变量提升,说白了那就是代码的声明提到代码的最前面

var name;name='jack';console.log(name);    //jack复制代码

其实这个变量提升应该是照道理接着编译原理写下来的,为什么放到了最后呢,因为如果你忘了,正好往上翻一下,重新回温一遍JS编译原理

紧接着上面,让我们来看下不吃变量提升这一套的 letconst ,先来看一段代码

name='jack';console.log(name)    //Uncaught ReferenceError: Cannot access 'name' before initializationlet name;复制代码

黑人问号 ??? ,说好的变量提升呢,记住 letconst 的一个特点,禁用变量提升,这也是 ES6 故意为之的,将生命前不可用做到了强约束,总结而言,** var 存在变量提升, letconst 不存在变量提升**

既然已经提到了 const ,顺带提一下它声明了以后必须赋值的操作

const name;    //Uncaught SyntaxError: Missing initializer in const declaration复制代码

暂时性死区

紧接着上面,让我们来看下什么叫做暂时性死区,先来看一段代码

var name='jack';{  name='bob';  let name;    //Uncaught ReferenceError: Cannot access 'name' before initialization}复制代码

记住 ES6 中的一个特性,如果区块中存在 letconst 命令,这个区块对这些命令声明的变量,从一开始就形成了封闭作用域。因为JS清楚地感知到了 name 是用 let 声明在当前这个代码块内的,所以会给这个变量 name 加上了暂时性死区的限制,它就不往外探出头了。

那么,如果我们把上面的let name;去掉,程序也将正常运行, name 的值也被成功修改为了bob,就是正常地按照作用域链的规则,向外探出头去了。

更多相关免费学习推荐:javascript(视频)

The above is the detailed content of Understand the compilation principles, scope, scope chain, variable promotion, and temporary Zenless Zone Zero of JS series (1) in one paper. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:juejin. If there is any infringement, please contact admin@php.cn delete
Python vs. JavaScript: Development Environments and ToolsPython vs. JavaScript: Development Environments and ToolsApr 26, 2025 am 12:09 AM

Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.

Is JavaScript Written in C? Examining the EvidenceIs JavaScript Written in C? Examining the EvidenceApr 25, 2025 am 12:15 AM

Yes, the engine core of JavaScript is written in C. 1) The C language provides efficient performance and underlying control, which is suitable for the development of JavaScript engine. 2) Taking the V8 engine as an example, its core is written in C, combining the efficiency and object-oriented characteristics of C. 3) The working principle of the JavaScript engine includes parsing, compiling and execution, and the C language plays a key role in these processes.

JavaScript's Role: Making the Web Interactive and DynamicJavaScript's Role: Making the Web Interactive and DynamicApr 24, 2025 am 12:12 AM

JavaScript is at the heart of modern websites because it enhances the interactivity and dynamicity of web pages. 1) It allows to change content without refreshing the page, 2) manipulate web pages through DOMAPI, 3) support complex interactive effects such as animation and drag-and-drop, 4) optimize performance and best practices to improve user experience.

C   and JavaScript: The Connection ExplainedC and JavaScript: The Connection ExplainedApr 23, 2025 am 12:07 AM

C and JavaScript achieve interoperability through WebAssembly. 1) C code is compiled into WebAssembly module and introduced into JavaScript environment to enhance computing power. 2) In game development, C handles physics engines and graphics rendering, and JavaScript is responsible for game logic and user interface.

From Websites to Apps: The Diverse Applications of JavaScriptFrom Websites to Apps: The Diverse Applications of JavaScriptApr 22, 2025 am 12:02 AM

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python vs. JavaScript: Use Cases and Applications ComparedPython vs. JavaScript: Use Cases and Applications ComparedApr 21, 2025 am 12:01 AM

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

The Role of C/C   in JavaScript Interpreters and CompilersThe Role of C/C in JavaScript Interpreters and CompilersApr 20, 2025 am 12:01 AM

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

JavaScript in Action: Real-World Examples and ProjectsJavaScript in Action: Real-World Examples and ProjectsApr 19, 2025 am 12:13 AM

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor