Table of Contents
Promotion behavior
Example analysis
Summary
Home Web Front-end JS Tutorial Analysis of JavaScript's lifting behavior and principles

Analysis of JavaScript's lifting behavior and principles

Feb 28, 2017 pm 02:43 PM
javascript

For students who have just learned JavaScript, some of its behaviors may make you confused

It is very different from the C/C++ you learned in school
For example, this is the case

a = 1;var a;console.log(a);// 1
Copy after login

Some students may think that it should be undefined, because var a seems to have been reassigned to a
In this case again

console.log(a);// undefinedvar a = 1;
Copy after login

a is printed before it is declared, it should be Is it correct to report an error, or refer to the behavior of the browser above and should output 2?
However, the result is undefined

The code sequence you see is not necessarily the actual execution sequence of the js engine
This is the JavaScript improvement behavior I want to talk about today
In fact, writing this article is to supplement the knowledge of precompilation
If you don’t know about precompilation before reading, please click below
JavaScript precompilation
JavaScript's precompilation directly leads to its promotion behavior

Promotion behavior

In fact, by understanding precompilation, we can clearly understand why
precompilation occurs before execution
So executing these two code blocks is equivalent to executing code like this

var a;a = 1;console.log(a);// 1
Copy after login
var a;console.log(a);// undefineda = 1;
Copy after login

For this promotion behavior, remember these two sentences
Variable declarations are promoted, and function declarations are promoted as a whole
I emphasize again,

function demo(){….}这才叫做函数声明 
var demo = function(){….}这不叫函数声明
Copy after login

In fact, as long as you understand the precompilation principle, the promotion behavior is easy to understand

We are used to treating var a = 1 as a statement
But the js engine doesn't think so. It will split it into var = a declaration and a = 1initialization
and the first one is the compilation phase task. The second is the task of the execution phase
So no matter where your declaration appears in the scope, it will be processed in advance before execution
This is equivalent to moving the variable declaration and function declaration to the end of the scope. Top
This process is called Lifting

We must not declare repeated variables during the programming process, let alone declare functions and ordinary variables with the same name
By the way, it is a good habit to declare variables with a var at the top of the scope (single var principle)
This way you don’t have to worry about any promotion behavior

Example analysis

Nonetheless , we still need to understand this process, it is also the focus of the interview questions
Let’s take an old-fashioned example and see it

function a(a, b){
    console.log(a);    console.log(c);
    c = 0;    console.log(b);
    var c;
    a = 3;
    b = 2;    console.log(a);    console.log(c);    console.log(b);    function b(){};
    console.log(b);
}
a(1);
Copy after login

I don’t know if you are confused, a bunch of abc
The correct answer is:

1   undefined    function b(){}    3    0    2    2
Copy after login

Let’s analyze it from pre-compilation first, and review it by the way

  1. Create AO object (Active Object)

  2. Look for function formal parameters and variable declarations within the function. The formal parameter names and variable names are used as attributes of the AO object, and the value is undefined

  3. The actual parameters and formal parameters are unified. Assign the actual parameter value to the formal parameter

  4. Look for the function declaration, the function name is used as an attribute of the AO object, and the value is the function reference

Finally function a The AO object generated before execution is roughly like this

//伪代码AO->{
    a: 1
    b: function(){}
    c: undefined}
Copy after login

So it is equivalent to executing such a function
(the function name a does not conflict with the variables inside)

function a(){    var a = 1;    var b = function(){};    var c;

    console.log(a);//1
    console.log(c);//undefined
    c = 0;
    console.log(b);//function(){}
    a = 3;
    b = 2;
    console.log(a);//3
    console.log(c);//0
    console.log(b);//2
    console.log(b);//2}
Copy after login

So we are very relaxed Got the answer

If there is a variable declaration and a function declaration with the same name
The function declaration must override the variable declaration (JavaScript functions are the first citizens with various privileges)
If a variable declaration has the same name as Another variable declaration has the same name
or a function declaration has the same name as another function declaration
That is whoever is under the document takes precedence

Summary

The above is the JavaScript to share with you today Promotion behavior
is summarized in two sentences: Variable declaration promotion, overall function declaration promotion
In fact, it is an inevitable behavior caused by js engine pre-compilation

The above is JavaScript To improve the content of behavior and principle analysis, please pay attention to the PHP Chinese website (www.php.cn) for more related content!


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

How to implement an online reservation system using WebSocket and JavaScript How to implement an online reservation system using WebSocket and JavaScript Dec 17, 2023 am 09:39 AM

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

How to use JavaScript and WebSocket to implement a real-time online ordering system How to use JavaScript and WebSocket to implement a real-time online ordering system Dec 17, 2023 pm 12:09 PM

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

How to get HTTP status code in JavaScript the easy way How to get HTTP status code in JavaScript the easy way Jan 05, 2024 pm 01:37 PM

Introduction to the method of obtaining HTTP status code in JavaScript: In front-end development, we often need to deal with the interaction with the back-end interface, and HTTP status code is a very important part of it. Understanding and obtaining HTTP status codes helps us better handle the data returned by the interface. This article will introduce how to use JavaScript to obtain HTTP status codes and provide specific code examples. 1. What is HTTP status code? HTTP status code means that when the browser initiates a request to the server, the service

How to use insertBefore in javascript How to use insertBefore in javascript Nov 24, 2023 am 11:56 AM

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).

See all articles