Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of variable Promotion and function Promotion in JavaScript

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the JavaScript variable promotion and function promotion method related knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe you will have something to gain after reading this JavaScript variable promotion and function promotion method article, let's take a look at it.

Js execution

Lexical analysis stage: including analysis of formal parameters, analysis of variable declaration, analysis function declaration of three parts. Through lexical analysis, the js code we wrote is transformed into executable code.

Execution phase

Variable lifting

Initialization will not be promoted only if the declaration is promoted

The declaration will be promoted to the top of the current scope

? 1:

Console.log (num) var numnum = 6

After pre-compilation

Var numconsole.log (num) / / undefinednum = 6

? 2:

Num = 6console.log (num) var num

After pre-compilation

Var numnum = 6console.log (num) / / 6

? 3:

Function bar () {if (! foo) {var foo = 5} console.log (foo) / / 5} bar ()

After pre-compilation

The declaration in function bar () {var foo / / if statement promotes the if (! foo) {foo = 5} console.log (foo)} bar () function

Both function declaration and initialization are promoted

Function expressions will not be promoted

? 1: function declarations can be promoted

Console.log (square (5)) / / 25function square (n) {return n * n}

After pre-compilation

Function square (n) {return n * n} console.log (square (5))

? 2: function expressions cannot be promoted

Console.log (square) / / undefinedconsole.log (square (5)) / / square is not a functionvar square = function (n) {return n * n}

After pre-compilation

Var squareconsole.log (square) console.log (square (5)) square = function () {return n * n}

? 3:

Function bar () {foo () / / 2 var foo = function () {console.log (1)} foo () / / 1 function foo () {console.log (2)} foo () / / 1} bar ()

After pre-compilation:

Function bar () {var foo foo = function foo () {console.log (2)} foo () / / 2 foo = function () {console.log (1)} foo () / / 1 foo () / / 1} function is promoted before variable promotion

? 1:

Console.log (foo) / / prints out the function function foo () {console.log ("foo")} var foo = 1

? 2:

Var foo = "hello" / / hello; (function (foo) {console.log (foo) var foo = foo | | "world" console.log (foo) / / hello}) (foo) console.log (foo) / / hello

After pre-compilation

Var foo = "hello"; (function (foo) {var foo foo = "hello" / / Foo value of the input parameter console.log (foo) / / hello foo = foo | | "world" / / foo has a value of hello, so it is not assigned to world console.log (foo) / / hello}) (foo) console.log (foo) / / hello, the order of var foo = "hello" JS variable promotion and function promotion under the global scope of printing

Recently, I came across the question of examining the order of variable promotion and function promotion in the recent written test. I only knew that the variables defined by var would be improved and the function declaration would also be improved, but there was no in-depth study of their order and detailed process. After consulting the data and my own verification, I got some understanding of their order. I didn't say much and went straight to the point.

First of all, I give my conclusion: the priority of function promotion is higher than that of variables, and it will not be overwritten when the variable of the same name is declared, but it will be overwritten after being assigned by the variable of the same name.

You can look at the following code:

Console.log (a) / / variable a () {} variable a prints the value of variable a before assignment; function a () {} console.log (a) / / 1 variable a prints the value of variable an after assignment

First of all, both the variable and the function declaration are promoted, but the function promotion priority is higher than the variable, and the variable is only defined without assignment, so the output is function a. The detailed process is as follows:

Function a () {} / / function declaration promotes a-> f a () {} var a; / / variable promotes console.log (a) / / at this time variable a simply declares that there is no assignment so it will not overwrite the function a-- > output function a f a () {} aq.1 / / variable assignment console.log (a) / / at this time variable an is assigned-- > the value of output variable an is 1

Summary: since both the function declaration and the variable are promoted, if the function has the same name as the variable, it will print the function before the variable assignment and the value of the variable after the variable assignment.

Now let's look at another piece of code:

A (); / 2 var a = function () {/ / as a function assignment to the variable a console.log (1)} a (); / 1 function a () {console.log (2)} a (); / / 1

In fact, I just want to tell you that only the function declaration will improve the function expression, so the code after the function expression will output 1, because the variable an is assigned to overwrite the promoted function a. The detailed process is as follows:

Function a () {/ / function promotion console.log (2)} var a; / / variable promotion a (); / 2 a = function () {/ / variable an assignment overrides the above function a console.log (1)} a (); / 1 a (); / 1

Look at another piece of code:

A (); function a () {console.log (1)} function a () {console.log (2)}

Print is 2, the reason is very simple, the first declaration will be overwritten by the later declaration.

This is the end of this article on how to improve variables and functions in JavaScript. Thank you for reading! I believe you all have a certain understanding of the knowledge of "variable promotion and function promotion in JavaScript". If you want to learn more knowledge, you are welcome to follow the industry information channel.

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report