JS Variable declaration Promotion

Source: Internet
Author: User

First, scope

In JS, the scope is the area within the function, called the functional scope.

Second, variable declaration

Before ES6, a variable was declared through Var, but after ES6, two more keywords were added to declare the variable: letand const

var: declares a variable that is scoped to the current execution context

Let: declares a local variable of a block-level field, and the variable it declares is valid only within the code block in which it resides

Const: A constant that declares a read-only block-level field, and the variable it declares is valid only within the code block in which it resides

1{//code block2   varA = 1;3Let B = 2;4Const C = 3;5   6Console.log (a);//17Console.log (b);//28Console.log (c);//39 }Ten  OneConsole.log (a);//1 AConsole.log (b);//error, REFERENCEERROR:B is not defined (...) -Console.log (c);//did not execute, because the above statement error, so this statement is no longer executed, if the above statement does not error, then here will be an error
1(function (){2   varA = 1;3Let B = 2;4Const C = 3;5   6Console.log (a);//17Console.log (b);//28Console.log (c);//39})();//for convenience, the self-executing function is used hereTen  OneConsole.log (a);//error, REFERENCEERROR:A is not defined (...) AConsole.log (b);//not executed -Console.log (c);//not executed

Let and const do not, like Var, can cause variable elevation phenomena

1 console.log (a);    // error, REFERENCEERROR:A is not defined 2 Let a = 2; 3 Console.log (a);    // Pending Execution

ES6 explicitly states that if a let and a const command exists in a chunk, the variables declared by the block on those commands form a closed scope from the outset. If you use these variables before the declaration, you will get an error.

Third, variable declaration promotion

JS performs a full parsing (including local variables) of the declaration portion of the entire script file before execution, thus determining the scope of the variable.

When a local variable has the same name as a global variable, the scope of the local variable overrides the scope of the global variable. When you leave the scope of a local variable, it goes back to the scope of the global variable.

1             varA=1;2             functionTest () {3alert (a);//undefined here A is not a global variable, the function has already declared a local variable name, so the global variable will be overwritten, JS execution before the declaration part of the resolution, so here's a only declared but not assigned value4A=4; Here A is assigned a value5alert (a);//4, where a is still a local variable6                 varA; Local variable A is declared here7alert (a);//48             }9 test ();Tenalert (a);//1, where A is a global variable
1     vara=10;2         functionTest () {3a=100;4alert (a);//100 Here A is a local variable, if there is no third row a=100, here is undefined, with the third row, where the local variable is assigned a value of5Alert This. a);//10 The inside of the function refers to the function caller, where the function is called by the global object, so this is a pointer to the global object (this refers to window)6             varA;7alert (a);//100/local variable8         }9Test ()

in basic statements (or blocks of code) (e.g.,,, if语句 , for语句 while语句 switch语句 , for...in语句 etc.), there is no promotion of variable declarations

Iv. function Declaration Promotion

function declarations and function expressions are syntactically equivalent, but a little different, the function declaration is referred to the top of the scope, and the function expression does not.

1Fun ();//Hello2 3 functionFun () {4Console.log ("Hello");5 }6 7 // --------------8 //after promotion9 Ten functionFun () { OneConsole.log ("Hello"); A } -  -Fun ();//Hello
1Fun ();//error, Typeerror:fun is not a function2 3 varFun =function(){4Console.log ("Hello");5 };6 7 // --------------8 //after promotion9 Ten varFun ; One  AFun ();//error, Typeerror:fun is not a function -  -Fun =function(){ theConsole.log ("Hello"); -};

JS Variable declaration Promotion

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.