And let the difference between variables defined var

A variable lift

var variables exist to enhance, upgrade and let the variable does not exist, so be sure to use in a statement after a let-defined variables, otherwise it will error.

where

// var defined variables exist variable lift, the variable declaration will enhance the overall scope of the foremost
 // example. 1 
the console.log (A);   // undefined 
var C = 10 ; 
 
// equivalent to 
var C; 
Console .log (A);   // undefined 
C = 10 ; 
 
/ * ------------------------ * / 
// example 2 
var A = 100 ; 
FUNC function () { 
    the console.log (A); // undefined 
    var A = 10 ; 
    the console.log (A); // 10 
} 
FUNC (); // call the function
 
// equivalent 
var A = 100 ; 
function FUNC () { 
    var A; 
    the console.log (A); // undefined 
    A = 10 ; 
    the console.log (A); // 10 
} 
FUNC (); // call function 
 
// Note:
 // for variables without declaration and direct assignment, equivalent to a global variable, it can be used after the assignment, before being given the assignment 
console.log (a); // error: a iS not defined 
a = 100 ; 
the console.log (A); // 100

let

// the let variables defined variable declaration does not exist, an error will be used before the variable declaration
 // example. 1 
the console.log (A);   // error Not defined IS A 
the let A = 100 ; 
 
// example 2 
function FUNC () { 
    the console.log (A); 
    the let A = 100 ; 
} 
FUNC (); // call the function

 

Second, the scope

var: Only function scope and concept of global scope, there is no concept of block-level scope.

let: the concept of block-level scope only by the up including {}, and for statements IF statement inside the {} belong block-level scope.

where

// global scope example
 // That is in addition to variables inside a function definition, others are global variables. 
for ( var I = 0 ; I < 100 ; I ++ ) { 
    ; 
} 
the console.log (I); // 100 
 
// function scope of example, the following outputs b 10, while being given a "a is not defined", why?
// Because the variable (b) to not declare and direct assignment, equivalent to a global variable.
// For the variable (a) assignment statement in the function, the function is only valid inside, outside inaccessible to avoid an error 
    
function FUNC () { 
    B = 10 ;
     var A = 100 ;         
} 
FUNC (); // function call 
the console.log (B);   //   10 
the console.log (A);  //报错  a is not defined
/ * Fill knowledge of 
    syntax of the function: call from function is defined and will call the function 
    (function () { 
            
    }) (); 
* / 
// examined as 
(function () {
     var A = B = 100 ;             
} ) (); 
console.log (b); 
console.log (A); 
 
// problem: a and b, respectively, what output it? 
 
 
// Answer: b outputs 100, while being given a "IS not a defined" 
 
// Why? 
 
// First, the function is decomposed into the familiar wording 
(function () {
     var A = 100 ; 
    B = 100 ;             
}) (); 
the console.log (B); //   100 
the console.log (A); //Not defined error IS A; 
 
// As a result, it can be seen, right? This is the previously mentioned "scope function": b corresponding to global variables, and a function to act only on the inside, not outside access.

let

As long as there is a block-level scope let command, variable declared it would "bind" (binding) in this area, no longer subject to external influences. In the code block, use the let command before declaring variables, the variables are not available, this is known as grammatically "temporary death"

// Example 1
 // being given "a is not defined", since if the code block prior to use let declare variables, the variables are not available, otherwise an error 
if ( to true ) { 
    A = 123 ; 
    let A ; 
} 
 
// example 2
 // being given "i is not defined", as defined using let i for the loop body only effective 
for (i = let . 1 ; i < 100 ; i ++ ) { 
    ; 
} 
the console.log (i )
 

 

 

Third, the repeated statement


var: variable can be declared more than once

let: Variable not allowed to repeat the statement, let not allowed in the same scope, the statement repeated the same variable. Inside the function can not be re-declare the same parameters

where

var A = 10 ; 
function FUNC () { 
    the console.log (A); // undefined 
    var A = . 11 ; 
    the console.log (A); // . 11 
} 
FUNC (); 
the console.log (A); // 10 
var a; 
the console.log (a); // 10 
var a = 12 is ; 
the console.log (a); // 12 is 
 
// this fact is related to the variable var variable declarations and repeatable lift
 // above can actually equivalent to 
var A; 
A = 10 ; 
function FUNC () { 
    var A;
    the console.log (A); // undefined 
    A = . 11 ; 
    the console.log (A); // . 11 
} 
FUNC (); 
the console.log (A); // 10 
 
the console.log (A); // 10 
A = 12 ; 
console.log (a); // 12 
 
// in this way, we believe that it is easy to arrive at the correct answer

let

// Example 1
 // being given "Identifier 'a' has already been declared" 
var a = 1 ; 
the let a = 2 ; 
 
// Example 2
 // not being given, because a different scope may be declared. 
A = the let ' Hello ' ; 
{ 
    the let A = ' Hi ' ; 
    the console.log (A); // Hi 
} 
the console.log (A); // Hello 
 
// example 3
 // being given "Identifier 'a' has already been declared ", as defined var variables upgrade will occur. 
A = the let ' Hello ' ; 
{
    var A = ' Hi ' ; 
    the console.log (A); 
} 
the console.log (A); 
 
// example 4
 // being given "Identifier 'A' has already been declared",
 // not duplicate the function declaration inside the same a parameter, as the default parameter variable is declared, it can not be declared const again using let or 
function FUNC (a) { 
    let a = ' Hello ' ; 
    the console.log (a); 
} 
FUNC ( ' Hi ' );

 

Reprinted https://blog.csdn.net/qq_41638795/article/details/81318704

 

Guess you like

Origin www.cnblogs.com/Tanghongchang/p/11007108.html