A little understanding of the rules and principles of variable promotion in JS

Source: Internet
Author: User

???? About the variable promotion, previously in a number of tutorials and books have been heard, peacetime development also know that the law, but today suddenly in a public class heard, the first reaction when a face confused, and then 100 degrees, instantly feel good familiar ah, almost by this concept to bluff, do not believe I give you see a chestnut, you will suddenly realize:

(function(){    console.log(v);    var v = ‘I love you‘;    console.log(v);})()// undefined  I love you

???? This is a typical example of a variable promotion, what is the rule, my understanding is that within a scope, no matter where you declare the variable will be promoted to the top, but will not be assigned a value. As in this example, V is first promoted to the top of the function scope, so the actual running process is this:

(function(){    var v = undefined;     console.log(v);    v = ‘I love you‘;    console.log(v);})()// undefined  I love you

???? Why is this, I refer to other people's explanation is because JS's operating mechanism:

JS top-down execution process is divided into two lexical analysis and execution of two stages: Lexical analysis includes: analysis of formal parameters, analysis of variable declaration, analysis function declaration three parts. Through lexical analysis, we write the JS code into executable code, the next step is to execute.

???? Variable promotion There is also a situation, that is, function, lexical analysis of the processing of function declarations and variable declaration processing is not very consistent, will be one step to the current function of the active object to increase the corresponding function name of the property, and override the method . That is, it does not assign a value undefined like a variable. Said a bit around, we still look at the code, you can first look at the results of the implementation, and then look at the following analysis:

function a(){    var b = ‘a‘;    function b(){       console.log(‘b‘)    }    alert(b)}a()

???? Simply put, the processing of function B in lexical analysis: Adds attribute B to the current function's active object, obj, and assigns a value. That is: obj.a = function () {...}; So the results of the lexical analysis are as follows:

function a(){    var b = undefined;    b = function b(){       console.log(‘b‘)    }    b = ‘a‘;    alert(b); // a}a()

???? This place is just a reference for me to read other people's opinions, but I'm a little bit unsure about who is at the top of the ordinary variable promotion and function promotion at the same time. Anyway, at present, my understanding of the law of variable promotion is clear, the principle of big lifting ascension is because of the problem of JS operation mechanism, in order to avoid some of the problems caused by variable promotion, the daily development of Chinese name or to form a good habit of declaring and using, try to declare all the variables at the beginning of the function. The views of the text are mostly personal understanding, if not, please point out!

Reference article: https://www.cnblogs.com/huilixieqi/p/6473572.html

A little understanding of the rules and principles of variable promotion in JS

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.