Js ES6 defined in the new variables

A, defines new variables for ES6
let const keywords define variables
1, let the variables defined:
Features:
  A, not pre-geocode results being given
  B, and () forEach similar variables, executed every time defined do not affect each other a new variable
  C, not redefine variable name, a variable name can be defined only once
  D, if the variables defined in {}, then the call can only be executed in {}, not elsewhere can. Therefore, different {}, the variable name can be defined in duplication with
  E, let only the best defined in the cycle
2, the definition of variables const:
Features:
  A, in js, the variable definition becomes constant const, can not be assignment is repeated, data has been defined, can not be modified
  B, const is defined in {}, {} can not be invoked outside
  C, const defined object, an array, a function, a reference data type. Wherein the reference data type as long as no change of address, reference can change type of data stored in the unit data
  D, const may be pre-parsed, the result is undefined

3, internal and external call demo:

// each cycle is independent, independently of each other corresponding to the value stored in the variable i 
var Olis = document.querySelectorAll ( 'Li' );
 for (i = the let 0; i <= oLis.length -. 1; i ++ ) { 
    Olis [I] .onclick = function () {
     // internally call is not a problem 
    the console.log (I +. 1 ) 
    } 
} 
// if the call defined variables let out, an error is reported 
console.log (i)

4, respectively, using var, let, const repetition assignment do comparison

  A, use var to declare variables

// use the variable declaration var 
var INT1 = 100 ;
 var INT1 = 200 ;
 // result is 200, before repeating the reset override 
console.log (int1);

  B, declare variables using let

// Use let you declare a variable 
let int2 = 300 ; 
let int2 = 400 ;
 // because the same variable twice let statement, it will be an error 
console.log (int2);

  C, using a const variable declaration

// use const variables declared 
const int3 = 500 ; 
const int3 = 600 ;
 // because the same variable, twice with const statement, it will be an error 
console.log (int3);

5, Special Uses:
A, although not outside the let {} call, but can directly call the function

function fun2 () { 
    the let STR = 'Shanghai' ;
     return STR; 
} 
// call the function, to output the result as 'Shanghai' 
the console.log (fun2 ());

B, const although once the assignment can not be changed. However, the array can be modified, the data stored in the object

// modify the array data, but does not modify the array 
const ARR = [1,2,3,4,5 ]; 
ARR [ 0] = 'Beijing'
 // output [Beijing, 2,3,4,5]; 
Console. log (arr);
// modify object data, but does not modify the object 
const obj = {name: 'John Doe' }; 
obj.name = 'John Doe' ;
 // output {name: John Doe} 
the console.log (obj)

Second, the function arrow: Another function of writing
1, the general function:

= fun1 function () { 
    function 
}

2, arrows functions:

fun2 = ()=>{}

3, characteristics
  A, and the general effect of performing functions identical
  B, and functions relating to function () replaced () =>
  C, if only one parameter can not write (), for example: e => {console.log ( E);}
  D, if only one line of code may not be written {}, for example: e => console.log (e)

Guess you like

Origin www.cnblogs.com/karl-kidd/p/12670262.html