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

What is the method of writing promotion in JavaScript scope

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

The main content of this article is to explain "what is the method of JavaScript scope writing and promotion". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "what is the method of JavaScript scope writing promotion"!

Promote what?

Moving the declaration of variables and functions to the top of the scope in which you are writing is called promotion.

The code is as follows:

Console.log (name); / / undefinedvar name = 'Rewa Fang';console.log (name); / / Rewa Fang

When you print a name for the first time, output the undefined instead of throwing an exception ReferenceError. It is precisely because the variable name declaration has been promoted. But only promote the declaration, the assignment will not be promoted; so output undefined. The second time you print, name has already been assigned to Rewa Fang.

Promoted code:

Var name;console.log (name); / / undefinedname = 'Rewa Fang';console.log (name); / / Rewa Fang

The code can also be changed to the following writing method:

Name = 'Rewa Fang';console.log (name); / / Rewa Fang var name

Var name; will be promoted to the top.

This is because the compiler finds all the declarations in the code during the compilation phase and binds them in the corresponding scope. Assignments and other logical code are left in place; waiting to be executed. For example: var a = 1; will be seen by the compiler as two parts declaring var a; and assignment a = 1;, then the declaration will be promoted to the top of the scope, and the assignment will be executed in place.

Including function declarations will also be promoted.

Function declaration promotes sayHi (); / / Hellogged function sayHi () {console.log ('hellographies');}

The function sayHi () executes normally; because the part of the function declaration has been promoted. Promoted to:

Function sayHi () {console.log ('hellograms');} sayHi (); / / Hello!

Variables and functions within the function are also promoted to the top of the function:

Var name = 'Lebron James';sayHi (); / / Hello! Rewa Fangfunction sayHi () {name = 'Rewa Fang'; console.log (' Hello!'+ name); var name;}

The name within the function is promoted to the top of the scope created by the function, so the external name is not referenced inside the function. The internal name obscures the external variable name.

After promotion:

Function sayHi () {var name;name = 'Rewa Fang'; console.log (' Hello!'+ name);} var name;name = 'Lebron James';sayHi (); / / Hello! Rewa Fang

One change after promotion is that function declarations take precedence over variable promotions.

For example:

Console.log (sayHi); var sayHi = 'Lebron James';function sayHi () {console.log (' Hello!');}

The result will output: "sayHi () {console.log ('Hello!');} output in node environment: [Function: sayHi]

Why?

Why do you need a promotion?

There are the following reasons:

Optimize performance; compiling the code before running will preprocess the declaration of variables and functions and manage the scope uniformly. Keep the code's top-down order variables declared before referencing. However, the order of the code can be artificially controlled, like Java does not need to improve the developer can effectively manage the declaration of variables. So this may not be the most important reason for promotion, or it may be a problem left over from history.

Mutual calls between functions; the following code, if function b is not promoted; the call in function a throws an exception; promotion makes functional programming more flexible.

Function a () {var a = 1; return b (a);} function b (num) {return num * 2;}

PS: let const will not be promoted in ES6

At this point, I believe you have a deeper understanding of "what is the method of writing promotion in JavaScript scope". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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

Development

Wechat

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

12
Report