ES6 Grammar (1) - let and const

ES6 Grammar ( 1 ) - the let and const

  let command

  ( 1 ) let you declare a variable, only let valid code block command is located, there is no variable lift, variable lift is to use the variable before you declare a variable, its value is undefined , but let declare variables can only be used after the variable declaration.

  ( 2 ) ES6 clear that if there is a block-level scope let and const command, this block these variables declared from the outset to form a closed area, who use these variables before the declaration would be an error.

  ( 3 ) not allowed to repeat declare variables.

  ( 4 ) block-level scope. ES6 only function scope and global scope, but let as JavaScript adds block-level scope:

  

function f1(){
        let n = 5;
        if ("1") {
            let n = 10;
        }
        console.log(n)
    }
    f1();  // n=5

 

  Inner code block is not affected by the outer code block.

  ( . 5 ) the ES5 predetermined function can only be declared only function scope and global scope, for ES6 introduces block-level scope and predetermined functions can be declared within the block-level action. And the function is similar to the role of the domain block level declared the let , not referenced outside the block-level scope:

 

function f() {console.log("i am in outside")};
(function(){
  if (false){
    function f(){
      console.log("i am in inside");
  }
}
f(); // Uncaught TypeError: f is not a function
}())

 

  In line with the above code ES6 error browser, Ruan Yifeng chiefs recommend using block-level role in the domain function expression to replace the function declaration.

  

  const command

 

  const declare a constant read-only, can not be changed once declared. And while the statement, you have to initialize immediately, otherwise it will error. Elsewhere, const and let use exactly the same.

 

const virtually guarantees that variable to point to the memory address shall not be altered. For simple data types, the value stored at the memory address pointed to by the variable, equal to the constant. For objects and arrays, variable points to the memory address, a pointer is stored, const ensured that the pointer is fixed, and a pointer to the data structure can not be controlled. In other words, const declared objects and arrays, objects and arrays are value can be changed.

 

Guess you like

Origin www.cnblogs.com/OnePieceKing/p/11608100.html