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

Js variable promotion and function promotion

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

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

Variables, as the most basic part of a programming language, vary from language to language, but with different paths. Variables in most programming languages have block-level scopes, such as if, for, while... But JavaScript is not purely in block-level scope, but in functional scope, and has its own unique feature-variable promotion. (ES6's newly added let, const make it available for block-level scope)

For the function variable access follows the scope chain, that is, the current function runtime will have a current scope, when drinking a variable, it will first find whether the definition of the variable exists in the current scope, if it does not exist, then find the scope of the parent function according to the scope chain, then use it, and if not, continue up to the global scope. The scope chain is not described in detail here. to put it simply, it is similar to the prototype chain, there is a mutually inclusive relationship from the global function to the scope of the current function, and the child can access up, but the parent cannot access the variables of the child function downwards. such a layer upon layer of nested relationship chain.

The chain of action is as follows:

Var num = 10; function a () {console.log (num);} a (); / / result alert (10). There is no num in the scope of a function, so look up the outer scope, there is and equal to 10, so pop up 10 instead of undefined.

Promotion of variables:

Var num = 10; function a () {/ / var num; console.log (num); var num = 11; / / num = 11;} a (); / / undefined

In this code, var num = 11 in function a () {}; split is equivalent to the commented out blue part, which is variable promotion-all variables are promoted to the header declaration of the scope of the function to which they belong.

Let's take a look at the question of function parameters, with a slight change in the first paragraph of code:

Var num = 10; function a (num) {console.log (num);

} a (); / / the scope of the undefined,a function defines the parameter num. Since there is no assignment, it is undefined.

Promotion of the function:

There are two ways of function declaration: function declaration and function expression, which will be different in function promotion.

Function promotion of function declaration:

Console.log (fn); / / function fn () {console.log (1);}

Function fn () {console.log (1);}

It is equivalent to the following code:

Function fn () {

Console.log (1)

}

Console.log (fn); / / function fn () {console.log (1);}

In a function declaration, the declared function as a whole is promoted to the top of the scope.

Function promotion of function expressions:

Console.log (fn); / / undefinedvar fn = function () {console.log (1);}

It is equivalent to the following code:

Var fn

Console.log (fn); / / undefined

Fn = function () {

Console.log (1)

}

In the function expression, similar to the variable promotion mentioned above, the variable from var is mentioned to be declared at the top of the scope.

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

Network Security

Wechat

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

12
Report