


Advanced front-end basics (2): Detailed illustration of execution context
We often encounter questions about assessment variable improvement in the early stages of learning JS or during interviews. For example, let’s start with a simpler one.
console.log(a); // 这里会打印出什么? var a = 20;
Ignoring this example for the time being, let’s first introduce the most basic but also the most important concept in JavaScript, execution context (Execution Context).
Every time the controller switches to executable code, it enters an execution context. Execution context can be understood as the execution environment of the current code, which forms a scope. The running environment in JavaScript roughly includes three situations.
1. Global environment: JavaScript code will first enter this environment when running
2. Function environment: When the function is called and executed, it will enter the current function to execute the code
3.eval
Therefore, in a JavaScript program, multiple execution contexts must be generated. As mentioned in my previous article, the JavaScript engine will process them in a stack manner. The bottom of the stack is always the global context, and the top of the stack is the currently executing context.
When the code encounters the above three situations during execution, an execution context will be generated and placed on the stack. After the context on the top of the stack is executed, it will automatically pop off the stack. In order to understand this process more clearly, we will show it to you based on the following examples and diagrams.
var color = 'blue'; function changeColor() { var anotherColor = 'red'; function swapColors() { var tempColor = anotherColor; anotherColor = color; color = tempColor; } swapColors(); } changeColor();
We use ECStock to represent the stack that handles the execution context group. We can easily know that the first step is to push the global context onto the stack.
After the global context is pushed onto the stack, the executable code in it begins to execute until it encounters changeColor(). This sentence activates the function changeColor to create its own execution context, so the first The second step is to push the execution context of changeColor onto the stack.
After the context of changeColor is pushed onto the stack, the controller begins to execute the executable code in it, and activates an execution context after encountering swapColors(). Therefore, the third step is to push the execution context of swapColors onto the stack.
In the executable code of swapColors, there is no other situation that can generate an execution context, so this code is successfully executed and the context of swapColors is popped from the stack. .
After the execution context of swapColors pops up, the executable code of changeColor continues to be executed, and no other execution context is encountered. After the execution is completed successfully, it pops up. In this way, there is only a global context in ECStack.
The global context is popped after the browser window is closed.
Note: In a function, encountering return can directly terminate the execution of the executable code, so the current context will be popped directly from the stack.
#After understanding this process in detail, we can summarize some conclusions about the execution context.
1. Single thread
2. Synchronous execution, only the context on the top of the stack is executing, other contexts need to wait
3. There is only one global context, it Pop the stack when the browser is closed
4. There is no limit to the number of execution contexts of a function
5. Every time a function is called, there will be a new execution context for it Create, even if it is a called function of itself.
In order to consolidate the understanding of execution context, let's draw the evolution of an example. This is a simple closure example.
function f1(){ var n=999; function f2(){ alert(n); } return f2; } var result=f1(); result(); // 999
Because the function f2 in f1 is not called and executed in the executable code of f1, f2 will not create a new context when f1 is executed, and one is not created until result is executed. new. The specific evolution process is as follows.

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.

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 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 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.

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 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.

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'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.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 English version
Recommended: Win version, supports code prompts!
