1, ES6 declare a variable manner

Declaratively 1, es5 of var problems

  1) is in the same scope var interior, can be repeated a variable declaration, behind the front will overwrite

  2) var variables exist to enhance the problem is before using var to declare variables is to use this variable

2, let the different characteristics of var

  1) let statement can not be repeated

  2) let is invariant to enhance the presence

  3) the temporary presence of block-level scope dead zone (ie, the area can not exist in a variable with the same name, this region is already bound to this variable)

  In es5 in parentheses if not a block-level scope, the following example is performed printing

IF ( to false ) {
   var lagev = 'jkgba' ;   
} 
the console.log (lagev); // undefined 

similar to the following: 
var lagev;
 IF ( to false ) { 
  lagev = 'jkgba' ;   
} 
the console.log (lagev); 

however when using the let statement, the presence of block-level scope, can only be accessed within the scope
IF (to false ) { 
  the let lagev = 'jkgba' ;   
} 
the console.log (lagev); // given, undefined

 

Another example is the for loop

for ( var i = 0; i <3; i ++ ) {
     // .... 
} 
the console.log (i); // this case i is a print out of 3, because i var declared a global variable, each time the same cycle i will use 


when used let do loop, each loop body is a block-level scope, each cycle will be re-declared a variable block-level scope of i, and the time will ,, i.e. initial value for each cycle i and the value of the loop current as the next binding statement 
for (the let i = 0; i <. 3; i ++ ) { 
    document.addEventListener ( 'the Click', function ( ) { 
        Alert (i);   // this case, i is the 0,1,2 
    }) 
} 

If this problem is the use es5 var achieved, then you need to use the closure, the preservation each value of i in fact, after using let compile also with the closure to achieve, so let's performance may be worse than some of the var,
but now Chrome's V8 engine performance is very powerful, and is constantly tweaked

3, es6 added a statement constants

  It can not be changed after the assignment statement, and let the same characteristics

  const screw = 10;

  ULIR = 2; // this case will be given, the type of error

  Generally is used to introduce the module project, we do not want to change

 

Guess you like

Origin www.cnblogs.com/gopark/p/11355054.html